0
0
Javaprogramming~10 mins

Loop initialization, condition, update in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Loop initialization, condition, update
Initialize loop variable
Check loop condition
NoExit loop
Yes
Execute loop body
Update loop variable
Back to Check loop condition
The loop starts by setting a variable, then checks a condition. If true, it runs the loop body, updates the variable, and repeats. If false, it stops.
Execution Sample
Java
for (int i = 0; i < 3; i++) {
    System.out.println(i);
}
Prints numbers 0, 1, and 2 by looping with initialization, condition check, and update.
Execution Table
Stepi (loop variable)Condition (i < 3)ActionOutput
10truePrint i=00
21truePrint i=11
32truePrint i=22
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 step 4 in the execution_table, so the loop exits.
When is the loop variable i updated?
After executing the loop body at each step, before checking the condition again, as shown in the variable_tracker.
Does the loop body run when the condition is false?
No, the loop body only runs when the condition is true. At step 4, condition is false, so the loop ends without running the body.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at step 2?
A2
B0
C1
D3
💡 Hint
Check the 'i (loop variable)' column at step 2 in the execution_table.
At which step does the loop condition become false?
AStep 3
BStep 4
CStep 1
DStep 2
💡 Hint
Look at the 'Condition (i < 3)' column in the execution_table to find when it is false.
If the update i++ was missing, what would happen to the loop?
AIt would run forever because i never changes
BIt would stop immediately
CIt would print only once
DIt would print numbers from 0 to 3
💡 Hint
Think about the variable_tracker and how i changes each step.
Concept Snapshot
for (initialization; condition; update) {
  // loop body
}

1. Initialization runs once at start.
2. Condition checked before each loop; if false, loop ends.
3. Loop body runs only if condition is true.
4. Update runs after each loop body execution.
Full Transcript
This example shows a for loop in Java with initialization, condition, and update. First, the variable i is set to 0. Then the loop checks if i is less than 3. If yes, it 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 step, showing i's value, condition result, action, and output. The variable_tracker shows how i changes after each iteration. Key moments clarify why the loop stops and when i updates. The visual quiz tests understanding of these steps.