skip to content

for Loop

The three-part for loop, comma-separated init and update expressions, and the for(;;) infinite form. Basic, but interviewers use scoping of the loop variable in output-prediction questions.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What are the three parts of a Java for loop, and in what order do they execute?

level: juniorimportance: must knowfreq 85%

answer

  1. init / condition / update separated by 2 semicolons
  2. init once; condition before each pass; update after body
  3. order: A,B,D,C,B,D,C...
  4. condition-first => can run zero times
  5. counter scoped to the loop

basics

~10 s

A 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 s

A 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 lines
java
for (int i = 0; i < 3; i++) {
    System.out.println(i); // prints 0, 1, 2
}
// i is not visible here

go deeper

for a junior

Can write a counting loop and state the init/condition/update roles and order.

for a middle

Explains zero-iteration behavior and counter scoping, and contrasts with while/do-while.

for a senior

Discusses readability, off-by-one risks, and when a for-each or stream reads better than an index loop.

for a principal

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

context

open as a page

When should you choose a for loop over a while loop, and why?

level: juniorimportance: should knowfreq 60%

basics

~20 s

Use a for loop when you know the number of repetitions or are counting over a range, because it keeps the counter setup, test, and update together. Use while when you just loop until some condition changes.

open as a page

How can a Java for loop initialize or update more than one variable, and what separator is used?

level: middleimportance: should knowfreq 55%

basics

~20 s

You can list several expressions in the init or update clause separated by commas, like for (int i = 0, j = 10; i < j; i++, j--). The comma lets one loop track two counters at once.

open as a page

What does for(;;) do in Java, and how do you exit such a loop safely?

level: middleimportance: should knowfreq 50%

basics

~10 s

for(;;) is an infinite loop: with all three clauses empty, there is no condition so it never stops on its own. You leave it with break, return, or by throwing an exception.

open as a page

In a Java for loop, what is the scope of a counter declared in the header, and what common off-by-one and update pitfalls should you watch for?

level: seniorimportance: should knowfreq 45%

basics

~20 s

A counter declared in the for header only exists inside the loop. Common bugs are using < vs <= wrongly (off-by-one), modifying the counter inside the body, and changing the collection size while looping by index.

open as a page