Code structure - loop or goto

rq3

Senior Member
Is there any particular benefit in terms of code size or execution speed between:

Main:
Do Stuff
goto main

and

Main:
do
Do Stuff
loop

Just curious. I know goto's are anathema to some.
 

AllyCat

Senior Member
Hi,

No, the Editor/Compiler will probably generate exactly the same code and AFAIK that will actually execute a GOTO (Jump to address). ;)

It's more complicated with a "Conditional Goto", which can exist in numerous forms: IF .. THEN .. ELSE .. ENDIF , IF .. THEN {GOTO} , ON .. GOTO (or BRANCH) , SELECT .. CASE , etc.. Personally, I prefer the IF .. THEN GOTO format because its execution time is completely predictable (the "True" path is about 50% slower than the "False" path); an ELSE adds "unpredictable" delays, and SELECT .. CASE even more. For a multiple branch, the ON .. GOTO is potentially the fastest, if the conditions involve a simple numerical sequence.

Cheers, Alan.
 

rq3

Senior Member
Hi,

No, the Editor/Compiler will probably generate exactly the same code and AFAIK that will actually execute a GOTO (Jump to address). ;)

It's more complicated with a "Conditional Goto", which can exist in numerous forms: IF .. THEN .. ELSE .. ENDIF , IF .. THEN {GOTO} , ON .. GOTO (or BRANCH) , SELECT .. CASE , etc.. Personally, I prefer the IF .. THEN GOTO format because its execution time is completely predictable (the "True" path is about 50% slower than the "False" path); an ELSE adds "unpredictable" delays, and SELECT .. CASE even more. For a multiple branch, the ON .. GOTO is potentially the fastest, if the conditions involve a simple numerical sequence.

Cheers, Alan.
Many thanks, Alan! You are a gentleman and a scholar.
 

westaust55

Moderator
If for any reason you might wish to exit the loop structure then the DO . . . LOOP structure also has available the EXIT command that will immediately jump to the next command after the LOOP statement.
 

RNovember

Well-known member
Is it good code practice to create several labels and use gotos at the end of each one to move to the next ones like this:
Code:
main:
    ;CODE
    goto sub1
    
flash:
    ;CODE
    goto sub2
    
sub1:
    ;CODE
    goto flash
    
sub2:
    ;CODE
    goto main
Here is a bit of code I'm working on where I do this.
 

Attachments

lbenson

Senior Member
Is it good code practice to create several labels and use gotos at the end of each one to move to the next ones like this:
It will work if you do it right, but no one will maintain that it is "good code practice". Functional is the best you can hope for with GOTO, and as programs grow larger, functionality becomes more tenuous.
 

AllyCat

Senior Member
Hi,
Is it good code practice to create several labels and use gotos at the end of each one to move to the next ones like this:
Hmm, IMHO better to structure the code correctly and minimise the number of GOTOs. ;) For example:
Code:
main:
    ; CODE
sub1:       ; Or just a comment to indicate the next process
    ; CODE  
flash:
    ; CODE
sub2:
    ; CODE
    goto main
;*   OR PERHAPS:
do
   call main
   call sub1
   call flash
   call sub2
loop
main:
    ; CODE
return
; ETC.....
A single GOTO main is better replaced by a do : loop (if only to keep the "GOTO-haters" happy), but IMHO multiple GOTO main:s (back to near the top of the program) is an acceptable way to "restart" the major part of a program after various alternative processes. But much more than that is venturing towards "Spaghetti Coding". If I need several paths to converge, I would generally arrange one path to "Fall Into" the label (with a comment to indicate that it wasn't a mistake) and minimise the number of GOTOs.

Personally, I do consider EXIT to be rather contrived, when it can mean either "GOTO the line after the next NEXT" OR "GOTO the line after the next ENDIF LOOP {condition}". But if I reach an (alternative) "exit point" part-way through a Subroutine, I do wonder whether to use a second RETURN (i.e. not at the end of the subroutine) or a GOTO ret {label before the terminal RETURN} for clarity.

EDIT: Corrected LOOP / ENDIF "typo". ;)

Cheers, Alan.
 
Last edited:

papaof2

Senior Member
I thimk I might make that last one GOTO EarlyReturn so it would be obvious in future reading - but then I've been known to add comments such as "Need to do xxx here in next version" in Version 0.1 of a new program.
 

Aries

New Member
Personally, I do consider EXIT to be rather contrived, when it can mean either "GOTO the line after the next NEXT" OR "GOTO the line after the next ENDIF".
I think that EXIT exits only from do...loop or for...next which are essentially equivalent constructions, and so it does the same thing in each case - i.e. exits the loop. Trying to use EXIT within an if block gives a compiler error (unless it happens to be within a loop, of course)
 

oracacle

Senior Member
Using exit in a nested loops will only exit to the previous loop as well and works very well when combined with an if statement. Combining this with the "do while" is also handy.

Code:
b0= 0
b1 = 100
Do while punc.0 =0"
Do while pinc.0 =0
   If b0>b1 then exit
   Inc b0
   Pause 10
Loop
'do something here
Loop
Just a loose example and could be done with a compounds while or if statement. Adjusting b1 will alter the timing and allow for a timer to be adjusted in increments while also allowing a pin to exit both loops. This could be used as a non blocking pause within a loop. So above is a pause of about 1000 without having to wait the entire to exit on pin c.0 to be detected.

You could also use for next on the inner loop
Code:
B1 = 10
Do while pinc.0=0
For b0 = 0 to b1
  If pinc.0 = 1 then exit
  Pause 10
Next b0
Loop
I suspect that trying to keep the exit command for unexpected events might be best but I'm really not qualified to say.
 
Top