goto and labels

BlitzMax Forums/BlitzMax Beginners Area/goto and labels

Eric Vaughn(Posted 2006) [#1]
Hello,

I'm learning Blitz Basic from the book "Game Programming for Teens". I have gathered that Blitz Max differs in some respects from the code used in the book.

The book has an example using goto and labels, here:

----------

.label
Print "Hello"
selection = Input("Enter 1 if you want me to repeat 'Hello' ==> ")
If (selection = 1)
Goto label
EndIf
End

----------

The label seems to be goofing up. Can someone tell me how to use gotos and labels in Blitz Max?

Thanks.


H&K(Posted 2006) [#2]
#label
Print "Hello"
selection = Int(Input("Enter 1 if you want me to repeat 'Hello' ==> "))
If (selection = 1)
Goto label
EndIf
End

BUT I never want to see you useing Goto ever again.


Eric Vaughn(Posted 2006) [#3]
Yeah, the book says that it is not good.


Grey Alien(Posted 2006) [#4]
yeah I advise programming using Strict, and when using Strict you are not allowed to use Goto anyway.


Grisu(Posted 2006) [#5]
Repeat
Print "Don't use labels and goto!"
Forever


Dreamora(Posted 2006) [#6]
Grisu: Thats quite wrong.

Don't use goto: right
don't use labels: trash. Want to see how you exit or continue nested loops without looplabels ;-)

Labels are VERY IMPORTANT, see the following code:

strict

#mainloop
repeat
  print "Mainloop running"
  #subloop
  while not keydown(key_escape)
    print "Subloop running"
    if keydown(key_s) then exit subloop
    if keydown(key_q) then exit mainloop
  wend
forever
print "Ending App"
end


But I assume you missed that little addition when it came in, right? :)


Grisu(Posted 2006) [#7]
I have stopped using labels and gotos since Basic for the C 64.

Imho you can do all that with functions and a programstate variable. I did that in Cardwar for instance.
Functions are easier to read and easier to debug too.
But everyone can have his own opinion about that.


H&K(Posted 2006) [#8]
Err I use labals for defdata does that mean Im wrong?

No Obviously not, its just dream not understanding English again

"Dont use Gotos and Labels" is NOT THE SAME AS "Dont use Gotos OR Labels"


GfK(Posted 2006) [#9]
If you can solve a problem using GoTo, then damn well use it.

There is no reason not to.


Grey Alien(Posted 2006) [#10]
if keydown(key_s) then exit subloop

this is news to me, saves setting a variable called ExitOuterLoop or something, then calling exit and checking the variable later. Thanks.


Yan(Posted 2006) [#11]
Use the wiki Luke!...


Dreamora(Posted 2006) [#12]
Grisu: Labels and Goto are 2 different things. Loop labels are a very powerfull and clean way to exit more than just 1 nested loop at once.
Not using such a thing as loop labels enforces loop variables and much stupid workaround.

Loop labels have NOTHING to do with Goto or any strange unclean codejumping.

H&K: He clearly stated that he meant it in the way he said, using any label / goto is bad which is just wrong. DefData is the other reason for labels thought I prefer external files to store data.


GfK(Posted 2006) [#13]
using any label / goto is bad which is just wrong
Why?


Warren(Posted 2006) [#14]
If you can solve a problem using GoTo, then damn well use it.

There is no reason not to.

100% agree. Sometimes goto is the cleanest solution as much as people might want to cry and scream to the contrary.


Dreamora(Posted 2006) [#15]
With Strict goto is no solution. And normally it isn't really needed especially since looplabels are there (because goto out of loops was the only really usefull thing, most other uses just base on "i've no idea on how to write clean efficient code without jumping around between statements")

Gfk: Because labels aren't bad. BM does not use them as jump point declarations only. As mentioned above, DefData and Looplabels use them as well and at least the later is a very powerfull tool to early drop and other important things, when processing data.


Barnabius(Posted 2006) [#16]
After 34 years of programming I am still unable to understand why the GOTO is so evil. I've seen and written many programs using GOTO and GOSUB and "dreaded" line numbers and they are all easily understood even today after all these years. Machine code uses GOTO's and after all, us people are using GOTO very often, don't we.

Sorry about this little outburst but I am getting mad when I see that a perfectly good and natural programming command is evil while many, many more C and C-like crap is praised to high heaven.

And some forgotten GOTO variants are just as useful (if not more) than what we are forced to use now. For example, I'd wish at least one modern language would have something called computed GOTO or computed GOSUB:

ON (iVar) GOTO Label1, Label2, Label3...

Barney


FlameDuck(Posted 2006) [#17]
Why goto is harmful. A history lesson.


Barnabius(Posted 2006) [#18]
Oh, please. Not Dijkstra and his anti-GOTO crusade...

Barney


Warren(Posted 2006) [#19]
GOTO is like any other tool. Powerful in the hands of the expert, dangerous in the hands of the novice.

Coders are perfectly capable of creating unreadable and horrific code without ever using a single GOTO statement. GOTO isn't the problem.


H&K(Posted 2006) [#20]
@Dream, Not he didnt, Dont use goto or labels means what you are claiming he said, whereas he said "Dont use Label And Goto"

OK fair enough its accedemic and he might have ment the meaning that you atributed to it. But equaly enough he may have ment the meaning that he acctualy wrote. And given the possibility dont you think you could have started your post with somthing other than "Grisu: Thats quite wrong."

Anyway GOTO is rubish, it doesnt work if you try to jump out of the wordspace, so you are stuck within the same function/code block anyway, and apparently it wont work in strict

There is on the other hand a Use for gosub, if gosub was limited to "Within" functions/Namespace then it would solve the "Fuction within Method problem that grey highlighted, that is we could have sub programs within methods.


Paposo(Posted 2006) [#21]
Barnabius.
If you have 34 years of experience using GOTO and you is unable to understand why goto is so evil, this is caused why you not read code from others programers. Is very very bad read code with GOTO's writed by other coders.

Bye,
Ramon


Barnabius(Posted 2006) [#22]
@Ramon: What you said is partially correct. Why partially? Because I've seen a lot of very badly written C and C++ code, which did not use GOTO's but were also completely unreadable to anyone who was not a creator of that particular code. As WarrenM (quite rightly) said:
Coders are perfectly capable of creating unreadable and horrific code without ever using a single GOTO statement. GOTO isn't the problem.


Barney


Warren(Posted 2006) [#23]
Is very very bad read code with GOTO's writed by other coders.

That's bull. If the code is hard to read that's the programmer's fault, not the GOTO command.


Paposo(Posted 2006) [#24]
WarrenM:
The goto dificlty the read everytime. Is need to read the code GOTOed for understand the fluxe program.
With funtions or gosubs is not needed.

Barnabius: Really have a lot of very bad code in C or other lenguaje. The existence of that code not invalidate my comentary. It expand this.

Bye,
Ramon


Warren(Posted 2006) [#25]
I won't be replying anymore since this argument is always a useless treadmill, but ...

It's not about being able to work around GOTO. Yes, you always can. But sometimes that's not the simplest answer. Sometimes it's just easier to use GOTO. When that's the case, there's nothing wrong with using it.

Having an irrational prejudice against a keyword in a language is just plain silly.


ImaginaryHuman(Posted 2006) [#26]
I say use Goto's if you want to.


CS_TBL(Posted 2006) [#27]
I never get into goto-potential situations whatsoever. I figure there's plenty o' room for improvements if you think you're about to use a goto.


SoggyP(Posted 2006) [#28]
Hello.

I think PRINT is a far worse command than GOTO, so nyeah.

Goodbye.


Boulderdash(Posted 2007) [#29]
while Selection=1
Print "Hello"
selection = Input("Enter 1 if you want me to repeat 'Hello' ==> ")
Wend

Now if GOTO is so bad then they should definitly remove it from X-86 all thoughs BRANCH commands must be bad!


Boulderdash(Posted 2007) [#30]
But they are right there is no reason on earth for you to use GOTO, you will get teased if someone sees a GOTO command.


H&K(Posted 2007) [#31]
Well...... As long as the goto doesnt "Go" more than a screen , (That is you can see the goto and the Label at the same time). I wouldnt say it was very bad. I would probably say that that bit of code was in Alpha thou.


grable(Posted 2007) [#32]
Banning goto just because you can write bad/unreadable code with it is stupid, by that analogy C/C++ should have been banned years ago!

gotos can be quite handy when you really need them, it saves you from coding in assembler to do the job (see Direct Threading feks)


Loktar(Posted 2007) [#33]
I see nothing "wrong" with goto's I personally don't use them because I can find ways around them but I remember when I was learning on the C64 thats all I used. My teacher called it spaghetti programming. Anyways since delving into VB after that I never used them but still dont see anything wrong if someone uses them and there program works flawlessly.


WendellM(Posted 2007) [#34]

Excessive use of Goto's/Gosub's (with short variable names and only rare comments due to memory limits) led to code as clear as mud back in the old days:

750 FOR I=1 TO K3:GOSUB 5200:A$=KL$:Z1=R1:Z2=R2:GOSUB 5240
760 K(I,1)=R1:K(I,2)=R2:K(I,3)=S9:NEXT I
770 IF B3=0 THEN 790
780 FOR I=1 TO B3:GOSUB 5200:A$=BA$:Z1=R1:Z2=R2:GOSUB 5240:NEXT I
790 IF S3=0 THEN 810
800 FOR I=1 TO S3:GOSUB 5200:A$=ST$:Z1=R1:Z2=R2:GOSUB 5240:NEXT I
810 IF K3<=0 THEN 840
820 IF RND(1)>.333 THEN 840
830 GOSUB 3690
840 GOSUB 4100

That's from one of the many Star Trek games on this site. Ahh, the memories... <g>


Dabz(Posted 2007) [#35]
sometimes GOTO is a fast,clean and efficent way of overcoming certain problems,which could involve extra programming.

if i need to use goto for a programming problem, which rarely i dont,but if i did, i try to keep the label and the goto statement in a small function,tucked out of the way.

for a long as i have been programming, goto arguements are just the same as repeat_until V while_wend arguements, inevitable pointless :-]

programming commands are tools to overcome programming problems,essentially,non of them should ever be knocked.

my two cents