While loop not breaking - Bug or by design?

I've got a workaround for it so I don't need help with the code, but when debugging a program I am working on, I noticed that the while loop isn't breaking when an interrupt is called during the loop changing the condition the loop is based upon. My understanding was that at any point during the loop if the condition changed the condition would be invalidated and the loop would stop? Or is the condition only evaluated at the end of the loop, aka } ?

acecombat: My understanding was that at any point during the loop if the condition changed the condition would be invalidated and the loop would stop? Or is the condition only evaluated at the end of the loop, aka } ?

From here:

while (expression) statement

The while-loop simply repeats statement while expression is true. If, after any execution of statement, expression is no longer true, the loop ends...

It might be important to declare the variables tested in the condition “volatile” so the test is not optimized away.

To skip the remainder of the loop and test the condition again, try “continue;”.

To end the loop without testing the condition again, use “break;”.

Yes, the condition is only checked between runs of the while loop.

The condition is not evaluated "after" or "between" iterations of the loop body. The condition is evaluated BEFORE every iteration of the loop - a very important distinction....

Regards, Ray L.

RayLivingston:
The condition is not evaluated “after” or “between” iterations of the loop body. The condition is evaluated BEFORE every iteration of the loop - a very important distinction…

Oops… Right. It’s the “do statement while (condition);” statement that tests at the bottom and thus always executes the statement at least once.

Sorry you are right RayLivingston, I should have said "re-evaluated at the end" as it obviously needs to evaluate before entering the loop otherwise the logic would just not be there.

The description Saximus posted was what I thought, but then looking at their example code the last line of the code is what changes the condition to be false so it's already at the stage it would break. I'll just have to keep in mind that it is only evaluating at the beginning or before it's next repeat.

Thanks for the info :)

acecombat: Sorry you are right RayLivingston, I should have said "re-evaluated at the end" as it obviously needs to evaluate before entering the loop otherwise the logic would just not be there.

The description Saximus posted was what I thought, but then looking at their example code the last line of the code is what changes the condition to be false so it's already at the stage it would break. I'll just have to keep in mind that it is only evaluating at the beginning or before it's next repeat.

Thanks for the info :)

"Re-evaluated at the end" isn't the case. "Evaluated at the beginning of every execution of the block" is.