Goto stuff.

1

PeskyBird 2020-04-24 11:33 (Edited)

I can get my program to go to a section if you do a specific input but another section if a different answer is typed (Kind of like a yes/no thing), but what does it mean by “variable not initialized”


Timo 2020-04-24 12:53 (Edited)

Before you can read a variable, it needs a value already.

PRINT V

...will give the error.

V=100
PRINT V

...will work.

Best practice is to initialize all global variables at the beginning of your program. It is also a good overview for all used global variables.


PeskyBird 2020-04-24 13:05 (Edited)

Thanks. What do I do when I set the variable in an input?


Timo 2020-04-24 15:06

INPUT should work fine. If you have problems, post your code here and I will check it.


PeskyBird 2020-04-24 15:14

PRINT "A)YES"
PRINT "B)NO"
INPUT ">";A$
PRINT
IF A$="A" THEN GOTO BUYSPAREPARTS ELSE IF A$="B" THEN GOTO OXEN

That is the section that is messing up.


Timo 2020-04-24 16:29

This part alone works fine, better show your complete program. you can attach it here.


was8bit 2020-04-24 16:43

This works....

PRINT "A)YES"
PRINT "B)NO"
INPUT ">";A$
PRINT
IF A$="A" THEN GOTO BUYSPAREPARTS ELSE IF A$="B" THEN GOTO OXEN


BUYSPAREPARTS:
CLS
PRINT "SPARE PARTS"

OXEN:
CLS
PRINT "OXEN"

Perhaps you dont have labels made or made correctly ?


PeskyBird 2020-04-24 17:01

Thanks


was8bit 2020-04-24 17:27

Let us know if you get your code running ok :)


crosoft312 2021-06-01 17:47 (Edited)

@The14doctor, It is best to not use goto, because most of the time it makes code hard to follow, and messy.
Instead, you replace it with do-loop, while-wend, if-else-endif, sub-endsub, etc...

print "a) yes"
print "b) no"
input ">"; a$
if a$="a" then
cls
print "spare parts"
else if a$="b" then
cls
print "oxen"
else
print "enter a or b"
end if

Itsn't it easier to read? or even better:

sub clear_and_print(t$)
cls
print t$
end sub

print "a) yes"
print "b) no"
input ">"; a$
if a$ = "a" then
print_and_clear("spare parts")
else if a$ = "b"
print_and_clear("oxen")
else
print_and_clear("enter a or b")
end if

There are some cases where goto can make code simpler, but when it can replaced with a loop or sub, it will make the code better.



Timo 2021-06-01 21:22

Crosoft, usually you are right. But in BASIC I still like GOTO as a simple way to jump between different parts of a program.

Let’s imagine we have different screens in a game (title, menu, ingame...) or situations/locations in a text game. The professional way of programming it would be probably a state machine. But to make a clean one, you would need a better programming language (with classes etc).

Also you wouldn’t program a classic text adventure with a “professional” language. You would write an interpreter for some kind of script language. And that script would probably have some kind of GOTO ;)

So I really think for some cases GOTO is a nice and simple solution. At least in LowRes NX.


Timo 2021-06-01 21:23

Ah, but yes, if other loop commands are possible, they are usually better than GOTO.


crosoft312 2021-06-02 00:44 (Edited)

You're right Timo, sometimes a direct jump is easier in a language like BASIC.

I was thinking about it, because I'm used to languages that have structs and classes and goto is obsolute. Sorry! I edited my post now, so it includes this info.

Thank you!


crosoft312 2021-06-02 17:32

Personally, I still try to avoid it, even if it's convenient.
I don't want to get bad practices when I go back to other langauges which have features that make goto absolute.


Timo 2021-06-02 21:47

In the end the important thing is that the code is easy to understand. I have seen a lot of code which was meant to be clean and well done, but actually was super complicated. But there is no “perfect” way, it’s a big part philosophy ;)


crosoft312 2021-06-02 22:11 (Edited)

This is true! Sometimes you need to do what's best for the situation, instead of following a guildline like "Don't use goto." You know what, I may use goto once or twice if it makes the code easier to read. Thanks for your advice!


was8bit 2021-06-03 05:41

Honestly, i write code for myself... sometimes i stick to my own standard approach... sometimes i get bored and explore trying to do something a different way ;)


crosoft312 2021-06-03 17:26 (Edited)

That sounds like a lot more fun! I might switch to that philosophy.


was8bit 2021-06-04 04:13 (Edited)

Ive always lived my life like that... i learn my own standards that help make life in general easier and or better... but then again i like creating my own path now and then ;)

For example, when i was a kid and had to mow the lawn, i really hated sweating under the sun on a saturday pushing an old lawn mower... BUT i made a game of it.. i imagined that the high growing weeds where little insect sky scrapers (grass being ordinary buildings) and i imagined the mower was a monster eating and destroying all the buildings... i would imagine the little insects trying to find refuge in the tallest buildings (the weeds) but the monster sepecially liked eating and chewing up those big buildings...

You could tell me what to do, but you cannot control my imagination ;) as a kid, it was constantly on... rather still is... there is no off switch unfortunately... ;)


crosoft312 2021-06-04 19:58 (Edited)

I used to do a similar thing, when I was a kid. Making up an imaginary situation to help the real one go down better. I think I used to when I had to rake the garden.

Of course, I've way out grown it, but it's interesting to remember.

I like your approach was8bit.




was8bit 2021-06-05 05:23

Thanky :)


crosoft312 2021-06-05 13:56 (Edited)

Oh! I didn't even look to see who was posting the comment! You posted the code with labels. And I ended up explaining the goto thing to you. Of course, you already knew. Sorry, I probably sounded aragent.


Log in to reply.