Using labels/GOTO?

I know using DELAY(); is frowned upon as is stops the rest of the program from running/executing..etc..

so it is best to use 'timers' to check when a specific function/task should be executed. (blink w/o delay..etc)

I was looking over a few noobie guides..etc.. and saw this section in the Programming & Syntax section:

repeat:
digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,LOW);
delay(1000);
goto repeat;

delay()'s aside...

how taboo is using a label like this?

example: how 'practical' (ok) is it to having some code running in your main loop().. doing whatever.... checkng sensors..buttons..(waiting for input).. and then based on a logic choice.. jump to one of these 'labels'.. (perfrom whatever task in there.. or just run inside that 'label' (routine) doing, whatever until it needs to break back out?

1.) how do you 'break' back out into your main loop? (or where ever the pointer was at before it jumped to the label/sub-routine?)

2.) how efficient (or frowned upon) is this??

Thanks

Using goto in this specific code is not very clever. A simple while(1) loop is cleaner and much more readable. Using goto is not taboo but in most cases not needed and entitled bad behaviour by most experienced programmers.

how do you 'break' back out into your main loop?

With another goto, but keeping the overview with lots of gotos is almost impossible.

how efficient (or frowned upon) is this??

It's quite efficient, usually one machine code statement.

thanks..

that isnt my code.. just the example I took to provide sample of what I was talking about. :slight_smile:

just the label/goto 'idea' itself.. (not the actual code in it)

I can see for 'debugging' it can be a PAIN to always jump to the label see what its doing..and 'follow the code' (so to speak)..

more of an annoyance, IMHO.. than a 'functional' problem though (related to performance or usability..etc)

So there is no 'return' or 'quit' command to automatically returns the pointer back to where it jumped out from?

thanks. :slight_smile:

You should never goto from a sub-routine to the main program (the compiler probably would mark that as an error, I guess) because that would clobber your stack and you run out of memory in no time.

There is absolutely nothing wrong with goto statements; however, they are not usually a good choice when using a high level language. Ultimately, most machine instruction sets ONLY have goto instructions rather than the other high level control structures more commonly used, so gotos can be more efficient from the machines perspective. However, in all but some situations, the additional optimization (which should be done in assembler anyway) isn't critical, and makes the code harder to maintain.

Ultimately readability is more important than efficiency in the vast majority of cases...