What are the three parts of a Java for loop, and in what order do they execute?
answer
- init / condition / update separated by 2 semicolons
- init once; condition before each pass; update after body
- order: A,B,D,C,B,D,C...
- condition-first => can run zero times
- counter scoped to the loop
basics
~10 sA for loop has three parts in parentheses, separated by semicolons: initialization, condition, and update. Init runs once first; then each pass checks the condition, runs the body, then runs the update.
solid answer
~40 sA Java for loop is written for (init; condition; update) { body }. The init runs exactly once before anything else. Then the condition is evaluated: if true, the body runs, then the update runs, and we loop back to re-test the condition; if false, the loop ends. So the order is init, then repeatedly (condition, body, update). The init typically declares a counter (its scope is limited to the loop), the condition is a boolean expression that decides whether to continue, and the update changes the counter so the loop eventually stops. Because the condition is checked before the first body execution, a for loop can run its body zero times if the condition is false from the start.
code
java · 4 linesfor (int i = 0; i < 3; i++) {
System.out.println(i); // prints 0, 1, 2
}
// i is not visible herego deeper
Can write a counting loop and state the init/condition/update roles and order.
Explains zero-iteration behavior and counter scoping, and contrasts with while/do-while.
Discusses readability, off-by-one risks, and when a for-each or stream reads better than an index loop.
Frames loop-construct choice as a team/readability convention and reasons about correctness invariants of the counter.
## What a `for` loop is A **loop** is a control-flow construct that repeats a block of code. A **`for` loop** in Java is the loop you reach for when you know (or can express) *how many times* or *over what range* to iterate, because it bundles the three pieces of loop bookkeeping into one header. ## The syntax ```java for (initialization; condition; update) { // body } ``` The header has **three clauses separated by two semicolons** (the semicolons are mandatory even if a clause is empty): 1. **Initialization** — runs **once**, before the loop starts. Usually declares and sets a *loop counter*, e.g. `int i = 0`. A variable declared here is **scoped to the loop only** — it does not exist after the loop ends. 2. **Condition** — a **boolean expression** evaluated **before every iteration** (including the first). If it is `true`, the body runs; if `false`, the loop stops immediately. 3. **Update** — runs **after each execution of the body**, then control returns to re-test the condition. Usually advances the counter, e.g. `i++`. ## Exact order of execution Given `for (A; B; C) { D }` the runtime does: 1. `A` (once) 2. evaluate `B` 3. if `B` is false -> exit the loop 4. run `D` (the body) 5. run `C` 6. go back to step 2 So the full sequence is: **A, B, D, C, B, D, C, B, ...** until B is false. ## A worked example ```java for (int i = 0; i < 3; i++) { System.out.println(i); } ``` Trace: init `i=0`; test `0<3` true -> print 0 -> update `i=1`; test `1<3` true -> print 1 -> update `i=2`; test `2<3` true -> print 2 -> update `i=3`; test `3<3` false -> stop. Output: `0 1 2`. ## Why the condition-first ordering matters Because the condition is tested **before** the first body run, a `for` loop can execute its body **zero times**. `for (int i = 0; i < 0; i++) { ... }` never enters the body. This is the key difference from a `do-while` loop, which always runs its body at least once. ## Terms defined - **Boolean expression**: an expression that evaluates to `true` or `false` (e.g. `i < n`). - **Counter / index variable**: the variable, often `i`, used to track progress. - **Scope**: the region of code where a variable is visible; a counter declared in the init clause is visible only inside the loop header and body. Understanding this exact order lets you predict how many times any `for` loop runs and where to read or change the counter.
- How does a for loop differ from a do-while loop in terms of minimum iterations?A for loop tests the condition first, so it can run zero times; a do-while runs the body once before testing, so it always runs at least once.
- What is the scope of int i declared in the for header?It is limited to the loop header and body; referencing i after the loop is a compile error.
saying these in an interview costs you the question
- Saying the update runs before the body
- Thinking the body always runs at least once (that is do-while)
- Believing the condition is checked after the body
- Assuming the loop variable is visible after the loop