Bird
Raised Fist0
Javaprogramming~10 mins

For loop syntax in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - For loop syntax
Initialize i=0
↓
Check: i < 3?
No→EXIT
Yes↓
Execute body
↓
Update: i = i+1
↩Back to Check
The loop starts by setting i to 0, then checks if i is less than 3. If yes, it runs the loop body, then increases i by 1 and repeats. If no, it stops.
Execution Sample
Java
for (int i = 0; i < 3; i++) {
    System.out.println(i);
}
This code prints numbers 0, 1, and 2 using a for loop.
Execution Table
Iterationi valueCondition (i < 3)ActionOutput
10truePrint 0, i++0
21truePrint 1, i++1
32truePrint 2, i++2
43falseExit loop
💡 i reaches 3, condition 3 < 3 is false, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
i01233
Key Moments - 3 Insights
Why does the loop stop when i equals 3?
Because the condition i < 3 becomes false at iteration 4 (see execution_table row 4), so the loop exits.
When is the value of i increased?
i is increased after the loop body runs each time (see execution_table Action column), before the next condition check.
What values of i are printed?
Values 0, 1, and 2 are printed during iterations 1 to 3 (see Output column in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i during iteration 2?
A0
B2
C1
D3
💡 Hint
Check the 'i value' column in execution_table row for iteration 2.
At which iteration does the loop condition become false?
AIteration 4
BIteration 3
CIteration 1
DNever
💡 Hint
Look at the 'Condition (i < 3)' column in execution_table.
If the loop condition changed to i <= 3, how many times would the loop run?
A3 times
B4 times
C2 times
D5 times
💡 Hint
Think about when i <= 3 is true for i values 0 to 4.
Concept Snapshot
For loop syntax in Java:
for (initialization; condition; update) {
    // code to repeat
}
- Initialization runs once at start.
- Condition checked before each loop; if false, loop ends.
- Update runs after each loop iteration.
- Loop body runs only if condition is true.
Full Transcript
This visual trace shows how a Java for loop works step-by-step. The loop starts by setting i to 0. Then it checks if i is less than 3. If yes, it runs the loop body which prints i, then increases i by 1. This repeats until i reaches 3, when the condition becomes false and the loop stops. The execution table tracks each iteration's i value, condition check, action, and output. The variable tracker shows how i changes from 0 to 3. Key moments clarify why the loop stops at i=3, when i increases, and which values print. The quiz tests understanding of i values, loop exit, and condition changes. The snapshot summarizes the for loop syntax and behavior in Java.

Practice

(1/5)
1.

What is the main purpose of a for loop in Java?

easy
A. To define a new class
B. To make decisions based on conditions
C. To repeat a block of code a specific number of times
D. To store multiple values in a list

Solution

  1. Step 1: Understand the role of a for loop

    A for loop is used to repeat code multiple times when the number of repetitions is known.
  2. Step 2: Compare with other options

    Options A, B, and C describe other programming concepts, not loops.
  3. Final Answer:

    To repeat a block of code a specific number of times -> Option C
  4. Quick Check:

    For loop = repeat code fixed times [OK]
Hint: For loops repeat code a set number of times [OK]
Common Mistakes:
  • Confusing for loop with if statement
  • Thinking for loop stores data
  • Mixing for loop with class definition
2.

Which of the following is the correct syntax for a basic for loop in Java?

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}
easy
A. for (int i = 0; i < 5; i++) { System.out.println(i); }
B. for int i = 0; i < 5; i++ { System.out.println(i); }
C. for (int i = 0; i < 5; i++) System.out.println(i)
D. for (int i = 0; i < 5; i++) System.out.println(i); }

Solution

  1. Step 1: Check the for loop syntax structure

    The correct syntax requires parentheses around initialization, condition, and increment, and braces to enclose the loop body.
  2. Step 2: Validate each option

    for (int i = 0; i < 5; i++) { System.out.println(i); } has correct syntax with parentheses and braces. for int i = 0; i < 5; i++ { System.out.println(i); } misses parentheses. for (int i = 0; i < 5; i++) System.out.println(i) misses semicolon after println(i). for (int i = 0; i < 5; i++) System.out.println(i); } has an extra closing brace.
  3. Final Answer:

    for (int i = 0; i < 5; i++) { System.out.println(i); } -> Option A
  4. Quick Check:

    Correct for loop syntax = for (int i = 0; i < 5; i++) { System.out.println(i); } [OK]
Hint: For loop needs parentheses and braces for multiple statements [OK]
Common Mistakes:
  • Omitting parentheses around loop control
  • Missing braces for multiple statements
  • Extra or missing braces
3.

What will be the output of the following Java code?

for (int i = 1; i <= 3; i++) {
    System.out.print(i + " ");
}
medium
A. 0 1 2
B. 1 2 3
C. 1 2 3 4
D. 3 2 1

Solution

  1. Step 1: Analyze the loop range

    The loop starts at i=1 and runs while i is less than or equal to 3, so i takes values 1, 2, and 3.
  2. Step 2: Determine the output

    Each iteration prints the current i followed by a space, so output is "1 2 3 "
  3. Final Answer:

    1 2 3 -> Option B
  4. Quick Check:

    Prints 1 to 3 with spaces = 1 2 3 [OK]
Hint: Check loop start, end, and print inside loop [OK]
Common Mistakes:
  • Starting loop at 0 instead of 1
  • Including extra number beyond 3
  • Printing in reverse order
4.

Identify the error in the following for loop code snippet:

for (int i = 0; i < 5; i++)
    System.out.println(i);
}
medium
A. Extra closing brace } without matching opening brace
B. Incorrect loop condition syntax
C. Missing semicolon after i++
D. Variable i not declared

Solution

  1. Step 1: Check loop syntax and braces

    The for loop has no opening brace but has a closing brace, which is unmatched and causes a syntax error.
  2. Step 2: Verify other parts

    Semicolons and variable declaration are correct, and the loop condition syntax is valid.
  3. Final Answer:

    Extra closing brace } -> Option A
  4. Quick Check:

    Unmatched brace causes syntax error = Extra closing brace } without matching opening brace [OK]
Hint: Match every opening brace with a closing brace [OK]
Common Mistakes:
  • Leaving unmatched braces
  • Forgetting semicolons in loop header
  • Not declaring loop variable
5.

How can you write a for loop in Java that prints only even numbers from 2 to 10 inclusive?

hard
A. for (int i = 0; i <= 10; i += 2) { System.out.print(i + " "); }
B. for (int i = 1; i < 10; i++) { if (i % 2 == 0) System.out.print(i + " "); }
C. for (int i = 2; i < 10; i++) { System.out.print(i * 2 + " "); }
D. for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); }

Solution

  1. Step 1: Understand the requirement

    We want to print even numbers from 2 to 10 inclusive, so numbers: 2, 4, 6, 8, 10.
  2. Step 2: Analyze each option

    for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } starts at 2 and increments by 2 up to 10, printing even numbers correctly. for (int i = 1; i < 10; i++) { if (i % 2 == 0) System.out.print(i + " "); } uses a condition inside the loop but loops only up to 9, missing 10. for (int i = 2; i < 10; i++) { System.out.print(i * 2 + " "); } prints multiples of 2 starting at 4 (2*2), but loop ends before 10 and prints wrong numbers. for (int i = 0; i <= 10; i += 2) { System.out.print(i + " "); } starts at 0, which is not in the required range.
  3. Final Answer:

    for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } -> Option D
  4. Quick Check:

    Increment by 2 from 2 to 10 = for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } [OK]
Hint: Use i += 2 starting at 2 for even numbers [OK]
Common Mistakes:
  • Starting loop at 0 instead of 2
  • Using wrong loop condition
  • Multiplying inside print instead of increment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard