WHich loop to use and when? do/while, for, else, if else, while, goto

septillion:
First the simple one, just never use goto :wink:

Next up, most loops can be transformed into each other but it might be handy to use a particular form.

For example, if you want to check if the outcome of some test is within limits it would be logical to use a do-while loop. Because he, you need to at least do the stuff one to know the outcome.

But if you want to loop over a set of unknown length, it's pretty stupid to first do something and only check afterwards. So while() or for() is more logical. Especially for() if you need to initiate something before you start (like a loop variable).

If, else and if else has nothing to do with looping :wink: It just decides if you want to do something or not :wink:

Thanks

I understand GoTo is not ideal, so how do I exit a loop that would take two different conditions?

Something like
Loop

Check to see if push button has been pressed.
If PB = High and Pin output = low exit go to routine A
If PB = High and Pin output = HIGH exit go to routine B
If no button press contine loop looking for button press.

GoTo would work, but what's better?