WHILE loop, when is/are the conditions checked?

Tested, maybe I am missing something.

IF (condtion), for example, a pin is low, do something. The Condition is checked before entering the ‘do something’. I found, if I change that condition, say that a pin I read goes low, it needed a high to enter the IF, it will not get rechecked until the next time through. Not in the middle of the ‘do something’ unless I recheck and force an exit.

So I tried a WHILE. While (condition), do something, and in that something, that condition changed. Assume again, I needed a pin to be high (while (digitalread pin 1 high)), and pin 1 goes low during the while instruction set, I found like the IF, the condition is not checked until the code in that While completes and the conditin is rechecked.

I ended up having to check that pin 1 in the middle of the While, or If, a couple times as the code was fairly long and I didn’t want it to finish until the condition was rechecked.

I am now using an ISR to check the pin 1, now pin 2 as int 0, wondering if I have this right? Did a few serial writes at the top and bottom to trace the code and when it exits, but I found no difference in using IF and WHILE in my case

Is there?

code ?

..if the rutine is slow, and u need immediate respons - use an interrupt. (as u found)

while () {...} is tested at the top/start of the loop
do {...} while (); is tested at the end of the loop

..if the rutine is slow, and u need immediate respons - use an interrupt. (as u found)

Bad advice.

If the condition is likely to change during the execution of the code then split the code up into smaller chunks that run faster and check the condition more often.

The challenge I had with the interrupt, is that I could not force that if, or while, to the end of that loop, and have it start over with a new condition.

The condition change happens the ISR. (A Button push, increment an integer), I tried chunking that loop into smaller pieces, i tried adding more checks in it to see if the counter changed, i even made contents of that loop a function, with a switch/case but again, although the ISR changes the counter, it is not tested until the next go around.

Thanks for the replies. I think in my case, making a few checks in the large loop for a changed counter or new flag, was the fix.

Sorry. The code is on the laptop somewhere else. It is a strobe controller with 6 functions, mode 7 is to do all 6 other patterns. The prob is, if one changes the mode via push button during mode 7, mode 7 had to complete before seeing the change and moving to mode 1. Mode 7 is about 8 seconds long.

Also are useful in for/do/while loops.