Can't leave "loop" with GOTO

In using GOTO control structure.

I understand that using this control structure is really not the way to go since
it can have issues especially if you have same keywords.

I would like to have a program running within "loop" and depending on the state of a
digital pin (low or high) I want the program to leave "loop" and start over from "setup".
If the label is within "loop" the GOTO compiles without errors but as soon as the label
is outside of "loop" the compiler generates errors. Being new to C/C++ I don't understand
why this creates compiling errors.

Other than using an interrupt (won't that have issues with delay() statement?) is there
a way to make this work? I just have not seen any examples of this.
Is it possible?


In using GOTO control structure.

We don't use GOTO here.


You can have jumps outside a function using setjmp() and longjmp(), but it's pretty easy to mess things up using them. I'm not dogmatic enough to say to never use goto, but whatever your coding choices are, goto is almost always in last place.

The big question is why you want to start over from setup() ? What does setup() do that you can't do in loop()?

You can call setup any time you want (except from setup itself - that would go south pretty quick).
Whether or not that’s the right thing to do, we can’t tell.

Write in assembly to jump to zero, crash the controller and let it restart.
Or just make a lot of new variables (using new), crash the controller and let it restart.

Otherwise make it clean. Make a function that has the code you want, call it in setup() and in loop().

depending on the state of a digital pin (low or high) I want the program to leave "loop" and start over from "setup".

That is not a sensible design philosophy.

Maybe have a look at Planning and Implementing a Program

Only use GOTO if you are sufficiently experienced at programming not to need advice about its use. (And I do realize that could be considered a circular argument). :slight_smile:


Ok so no need to use GOTO since I was able to use an "if" statement and all is working
as expected now within the "loop".

In assembly language I have used short or long jumps before but was not sure how to do
this in C/C++.

TNX all the help.

BTW if you're in a WHILE {} or DO {} LOOP UNTIL structure - you can use break; to jump out of the current level of iteration.

Suggestions above are the right way togo - not goto...!

If you were programming in C anywhere other than Arduino - there are no pre-defined SETUP() or LOOP() functions.