break
suggest changeSometimes loop condition should be checked in the middle of the loop. The former is arguably more elegant than the latter:
for (;;) { // precondition code that can change the value of should_end_loop expression if (should_end_loop) break; // do something }
Alternative:
bool endLoop = false; for (; !endLoop;) { // precondition code that can set endLoop flag if (!endLoop) { // do something } }
Note: In nested loops and/or switch
must use more than just a simple break
.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents