Multi-exit Loop
A multi-exit loop (or mid-test loop) has one or more exit locations within the body of the loop, not just at the top (while) or bottom (do-while).
Why?
Some loops have natural exit points mid-iteration. Forcing the exit condition into the
whileheader either requires evaluating work twice or introduces flag variables that obscure control flow.
for ( ;; ) { // infinite loop, while ( true )
if (...) break; // middle exit
}A loop exit NEVER needs an else
This pattern pulls the continuation out of the conditional — the loop body, not the if, is what matters.
Bad
for ( ;; ) {
if ( C1 ) { S2 }
else { break; }
...
}Good
for ( ;; ) {
if ( !C1 ) break;
S2;
...
}S2 is logically part of the loop, not part of a conditional — keep it at loop scope.
Multiple exits with different epilogues
Each exit can run its own finalization before breaking, which is much cleaner than encoding the same logic with flag variables:
for ( ;; ) {
S1
if ( C1 ) { E1; break; }
S2
if ( C2 ) { E2; break; }
S3
}