UNIT 2: SELECTION AND ITERATION · TOPIC 2.8
2.8 for Loops
The for loop packages initialization, condition, and update into one header. It's equivalent to a while loop, and the exam expects you to convert between them.
What you need to know
for (init; condition; update) { body }. Execution: init once → check condition → body → update → check condition → … until the condition is false.- A variable declared in the init (
int i = 0) exists only inside the loop. Using it after the loop is a compile error. for (int i = 0; i < n; i++)runs exactly n times, i = 0 through n−1.i <= nruns n + 1 times.- Number of iterations for
i = a; i < b; i++is b − a (if b > a). Fori <= b, it's b − a + 1. - The update can be anything:
i += 2,i--,i *= 2. Counting down:for (int i = 10; i > 0; i--). - Any for loop can be rewritten as a while loop by moving the init above and the update to the end of the body. The difference: scope of the loop variable.
- Modifying the loop variable inside the body is legal but confusing and a source of trick questions.
Worked example
for (int i = 2; i <= 10; i += 3)
{
System.out.print(i + " ");
}
// prints: 2 5 8
// i would be 11 after, but i doesn't exist outside the loop
// equivalent while loop:
int i = 2;
while (i <= 10)
{
System.out.print(i + " ");
i += 3;
}
System.out.println(i); // 11 — i is in scope here
Exam tip: Count iterations by listing the loop variable's values: start, add the step until the condition fails. Don't compute a formula unless the step is 1. And remember:
< vs <= is one full extra iteration.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Execution order of
for (init; cond; update): init once → cond → body → update → cond → body → update → … → cond false → exit. The update runs after the body, before the next check. - Loop variable scope: a variable declared in the init exists only inside the loop. After the loop it's gone — using it is a compile error. To use it after, declare it before the loop and initialize in the init:
int i; for (i = 0; …). - Iteration counts:
i = a; i < b→ b − a iterations.i = a; i <= b→ b − a + 1. With step s: ⌈(b − a) / s⌉ for<. When in doubt, list the values. - Counting down:
for (int i = n; i >= 0; i--)runs n + 1 times (n down to 0).i > 0runs n times (n down to 1). The exam tests the boundary. - Multiplicative steps:
for (int i = 1; i < 100; i *= 2)→ 1, 2, 4, 8, 16, 32, 64: seven iterations. No formula; list them. - for ↔ while conversion:
for (A; B; C) { D }≡A; while (B) { D; C }. The only difference is the loop variable's scope. Equivalence questions between the two forms hinge on the placement of C (must be at the end of the body) and on scope. - Modifying the loop variable inside the body is legal and confusing. If the body does
i += 2and the header doesi++, i increases by 3 per iteration.
Mistakes that cost points
- < vs <= off-by-one. One full extra iteration. Count the endpoints.
- Using the loop variable after the loop. Out of scope if declared in the header.
- Converting for to while with the update at the top of the body. It goes at the bottom, or the first iteration sees the wrong value.
- Applying a formula to a non-unit step. List the values instead.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 What is printed by the following code?
for (int i = 10; i > 0; i -= 4)
{
System.out.print(i + " ");
}Show answer
Answer: A. 10 (print), 6 (print), 2 (print), -2 fails i > 0.
Q2 How many times does the body of
for (int j = 3; j <= 15; j++) execute?Show answer
Answer: B. j takes values 3 through 15 inclusive: 15 - 3 + 1 = 13.
Q3 Which of the following will cause a compile-time error?
Show answer
Answer: A. i declared in the for header is out of scope after the loop. Option B declares it outside, so it's fine.
Key vocabulary
- for loop
- a loop with initialization, condition, and update in its header
- Loop variable
- the counter declared in the for header; scoped to the loop
- Scope
- the region of code where a variable can be used