Precisely where does control transfer for break and continue in for, while, and do-while loops, including the for loop's update expression?
answer
- break -> after the loop, always
- for: continue runs update (i++) then condition
- while: continue -> condition directly
- do-while: continue -> bottom condition
- for-each: continue -> next element
basics
~20 sbreak sends control to right after the loop in all loop types. continue goes to the loop's condition check, but in a for loop it runs the update part (like i++) first. So a for loop's update always runs on continue; a while/do-while jumps straight to the condition.
solid answer
~40 sbreak, in for, while, and do-while, transfers control to the statement immediately following the loop; the condition is not re-tested. continue ends the current iteration but where it lands differs by loop type. In a for loop, continue executes the update expression (e.g. i++) and then re-evaluates the loop condition. In a while loop, continue jumps directly to the condition test. In a do-while loop, continue jumps to the condition test at the bottom (which then decides whether to loop again). This distinction is why a for loop's counter always advances on continue, but a while loop whose increment sits below continue can loop forever. Knowing the exact transfer point prevents off-by-one and infinite-loop bugs.
go deeper
Knows break ends the loop and continue skips an iteration, but may be fuzzy on exactly where continue lands.
States precisely that for-continue runs the update then tests condition, while/do-while-continue goes straight to the condition.
Uses this precision to debug and prevent off-by-one and infinite-loop issues and explains the for-vs-while difference to others.
Encodes such mechanics in coding standards and reviews, steering toward loop forms least prone to these subtle bugs.
## Why the exact transfer point matters `break` and `continue` are *jump statements* — they move execution somewhere other than the next line. To reason about loops correctly you need to know **exactly** where each lands, because the difference between loop types causes real bugs. ## break — same everywhere In every loop type (`for`, `while`, `do-while`), `break` transfers control to the **first statement after the loop body**. The loop condition is **not** re-evaluated, and the for loop's update expression does **not** run. The loop is simply done. ## continue — depends on the loop type `continue` ends the *current iteration* but the landing spot differs: ### for loop The canonical for loop has four parts: `for (init; condition; update) body`. On `continue`, control goes to the **update** expression, runs it, then evaluates **condition** to decide on another iteration. ```java for (int i = 0; i < 5; i++) { // update i++ runs on continue if (i == 2) continue; // -> runs i++, then tests i < 5 System.out.println(i); // 0,1,3,4 } ``` Because the update runs, the counter always advances — no infinite loop. ### while loop `while (condition) body`. On `continue`, control goes **straight to condition**. There is no separate update slot. ```java int i = 0; while (i < 5) { i++; // must advance BEFORE continue can skip it if (i == 3) continue; // -> jumps to (i < 5) System.out.println(i); } ``` If the increment were placed *after* the `continue`, it would be skipped on that iteration, risking an infinite loop. This is the most common while-loop bug. ### do-while loop `do body while (condition);`. The body always runs at least once. On `continue`, control goes to the **condition at the bottom**, which then decides whether to run the body again. ```java int i = 0; do { i++; if (i == 3) continue; // -> jumps to while (i < 5) System.out.println(i); } while (i < 5); ``` ### enhanced for (for-each) `for (T item : collection)`. There is no visible counter; `continue` simply advances to the next element, and `break` leaves the loop. No infinite-loop risk because the iterator advances each pass automatically. ## Mental model Think of `continue` as "go to the *next condition check*, doing whatever the loop normally does between iterations." For a for loop, that between-iteration work is the update expression; for while/do-while there is none, so it goes straight to the condition. ## Summary - break → after the loop, no condition re-check, no for-update. Same in all loop types. - continue → for: run update, then test condition; while/do-while: go straight to condition. - Place the counter increment *before* a continue in a while loop, or use a for loop, to avoid infinite loops.
- Does break in a for loop run the update expression (i++) before leaving?No. break leaves the loop immediately; neither the update expression nor the condition runs again.
- How do you safely use continue in a while loop without risking an infinite loop?Make sure whatever advances the loop (e.g. the increment or reading the next item) happens before the continue, or use a for loop whose update runs automatically on continue.
saying these in an interview costs you the question
- Thinking the for loop's update is skipped by continue (it runs)
- Thinking break re-checks the condition or runs the for update
- Placing the increment after continue in a while loop (infinite loop)
- Assuming continue and break land in the same place