how to exit immediately from a set of loops???

I have a program where it's very important to exit a while loop program containing a pile of sub for loops immediately as a digital input goes low. I guess what that means is that I need it to NOT finish the execution of the remainder of whats in the While loop and immediately exit in real time response to an input going from high to low.

Right now I have a

while(pin xxxxxx == HIGH)
{
..... bunch of loops in the program
}

and it starts the code but it finishes out the entire while loop contents before it reads the pin again. I even call to read the pin at the first line of code in the while loop. But something's not quite working.

Any tips?

Could you post your current code? As often by rearranging the logic you don't need such a jump. maybe you need to abstract an inner loop into a function();

Don't use goto unless there are serious^2 reasons to do so. http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html

Better options are to use break or continue statements, another option might be return

break: http://msdn.microsoft.com/en-us/library/wt88dxx6(VS.80).aspx
continue: http://msdn.microsoft.com/en-us/library/6e3dc2z3(VS.80).aspx

Using return within setup():

  • exits setup and starts with loop()

Using return within loop():

  • exits loop() and starts with loop() again
void loop() [
      while (yourCondition1 && loopOK) {
            while (yourCondition2 && loopOK) {
                  while (yourCondition3 && loopOK) {
                        if (true) { //some BAILOUT condition
                              loopOK = false;
                              break;
                        }
                  }
            }
      }
}

Something along those lines?

If it works in your code, then use goto. May be better than using bunches of breaks. The old hens club usually acts like the sky is falling when goto is mentioned, but that is probably because they have difficulty wrapping their head around code flow logic (computershave absolutely no problem with it).

If its one pin or two just use an interrupt and handle that pin in interruptions, as said in the avrfreaks if the while loop is long you need multiple checks along the code.

The old hens club usually acts like the sky is falling when goto is mentioned, but that is probably because they have difficulty wrapping their head around code flow logic (computershave absolutely no problem with it).

That's because the old hens club have read their K&R, and if the very authors of "The C Programming Language" disagree with the use of goto, that's a very strong warning, and one that should be heeded carefully.

The problem isn't knowing when to use goto, its in knowing when not to. Its very easy to abuse the power of goto; this is why we old hens argue that it should remain somewhat under wraps. The goto statement was included in BASIC, and the spaghetti code that was written was utterly unbelievable. Perhaps we don't want to see that happen to C/C++.

Honestly, if your code includes a ton of nested loops, its as likely as not that you are applying the wrong design pattern to the problem at hand; you likely would be better off with a state machine pattern in many cases. Regardless, though, any time your code seemingly becomes overly complex (like, when you feel a need to use goto, for instance), that's a strong indication that refactoring is needed. Take a step back (and maybe a break), and recheck your design.

/IMHO - but willing to listen to arguments otherwise... :slight_smile:

Do I need to put the GOTO in every single loop within the program? seems like I included it in the main while loop (houses all the other loops) and it works but it finishes what it's doing in the loop first (not immediate).

Do I need to put the GOTO in every single loop within the program? seems like I included it in the main while loop (houses all the other loops) and it works but it finishes what it's doing in the loop first (not immediate).

Without posting your code, how do you expect anybody to have any idea what it needs?

To be fair, the only answer to the question "how to exit immediately from a set of loops???" is:

Use goto.

I very much agree with cr0sh in that a redesign probably is in order.

I really think that AlphaBeta's code in Reply #2 is far better than using goto. You can use more than one conditional, if necessary.

I also agree with cr0sh and AlphaBeta that if you are struggling with HOW to exit several deeply nested loops, that a redesign is seriously overdue.

Putting code into functions may help eliminate the need for the level of nesting you are trying to back out of.