Bird
Raised Fist0
Javaprogramming~10 mins

Loop execution flow in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Loop execution flow
Initialize loop variable i=0
↓
Check condition i < 3?
No→EXIT loop
Yes↓
Execute loop body
↓
Update loop variable i = i + 1
↩Back to condition check
The loop starts by setting a variable, checks if it meets a condition, runs the loop body if yes, updates the variable, and repeats until the condition is false.
Execution Sample
Java
for (int i = 0; i < 3; i++) {
    System.out.println(i);
}
This code prints numbers 0, 1, and 2 by looping while i is less than 3.
Execution Table
Stepi valueCondition (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 - 2 Insights
Why does the loop stop when i equals 3?
Because the condition i < 3 becomes false at step 4, so the loop exits as shown in the execution_table row 4.
When is the value of i updated during the loop?
i is updated after the loop body runs, before the next condition check, as seen between steps 1-2, 2-3, and 3-4 in the variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at step 3?
A2
B3
C1
D0
💡 Hint
Check the 'i value' column in execution_table row 3.
At which step does the loop condition become false?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Condition (i < 3)' column in execution_table where it changes to false.
If the loop condition was changed to i <= 3, how many times would the loop run?
A3 times
B4 times
C2 times
D5 times
💡 Hint
Consider the variable_tracker and how the condition affects loop continuation.
Concept Snapshot
for (int i = 0; i < limit; i++) {
  // loop body
}

1. Initialize i
2. Check condition (i < limit)
3. Run body if true
4. Increment i
5. Repeat until condition false
Full Transcript
This example shows how a for loop runs in Java. It starts by setting i to 0. Then it checks if i is less than 3. If yes, it prints i and increases i by 1. This repeats until i is 3, when the condition fails and the loop stops. The execution_table shows each step with i's value, condition result, action, and output. The variable_tracker follows i's changes. Key moments explain why the loop stops and when i updates. The quiz tests understanding of i's value at steps, when the loop ends, and effects of changing the condition.

Practice

(1/5)
1. What does a for loop in Java primarily do?
easy
A. Executes code only once
B. Stops the program immediately
C. Repeats a block of code a specific number of times
D. Creates a new variable

Solution

  1. Step 1: Understand the purpose of a for loop and compare options

    A for loop is designed to repeat a block of code multiple times based on a condition. Only "Repeats a block of code a specific number of times" correctly describes it.
  2. Final Answer:

    Repeats a block of code a specific number of times -> Option C
  3. Quick Check:

    for loop = repeat code [OK]
Hint: For loops repeat code a set number of times [OK]
Common Mistakes:
  • Thinking for loops run only once
  • Confusing loops with variable declarations
  • Believing loops stop the program
2. Which of the following is the correct syntax for a basic while loop in Java?
easy
A. while (condition); { // code }
B. while condition { // code }
C. while: (condition) { // code }
D. while (condition) { // code }

Solution

  1. Step 1: Recall Java while loop syntax and check options

    The correct syntax requires parentheses around the condition and braces for the code block. Only "while (condition) { // code }" matches: while (condition) { // code }.
  2. Final Answer:

    while (condition) { // code } -> Option D
  3. Quick Check:

    while loop syntax = parentheses + braces [OK]
Hint: While loops need parentheses and braces [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Adding semicolon after while condition
  • Using colon instead of parentheses
3. What will be the output of this Java code?
int count = 0;
while (count < 3) {
  System.out.print(count + " ");
  count++;
}
medium
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. 3 2 1

Solution

  1. Step 1: Analyze the loop condition and trace iterations

    The loop runs while count is less than 3, starting from 0. It prints 0, then increments to 1; prints 1, increments to 2; prints 2, increments to 3; then stops because 3 is not less than 3.
  2. Final Answer:

    0 1 2 -> Option A
  3. Quick Check:

    Prints 0,1,2 then stops [OK]
Hint: Count starts at 0 and stops before 3 [OK]
Common Mistakes:
  • Including 3 in output
  • Starting count from 1
  • Printing in reverse order
4. Identify the error in this loop code:
int i = 0;
while (i < 5) {
  System.out.println(i);
}
medium
A. Missing increment of i inside the loop
B. Incorrect loop condition syntax
C. Missing semicolon after while
D. Variable i should be declared inside the loop

Solution

  1. Step 1: Check loop control variable and consequences

    The variable i is initialized but never changed inside the loop. Without incrementing i, the condition i < 5 is always true, causing an infinite loop.
  2. Final Answer:

    Missing increment of i inside the loop -> Option A
  3. Quick Check:

    Loop needs variable update to stop [OK]
Hint: Always update loop variable to avoid infinite loops [OK]
Common Mistakes:
  • Forgetting to increment loop variable
  • Misplacing semicolons
  • Declaring variable inside loop unnecessarily
5. Consider this code snippet:
int sum = 0;
for (int i = 1; i <= 5; i++) {
  if (i == 3) continue;
  sum += i;
}
System.out.println(sum);

What is the output and why?
hard
A. 15 because all numbers 1 to 5 are added
B. 12 because number 3 is skipped in addition
C. 10 because only numbers less than 3 are added
D. Error due to incorrect use of continue

Solution

  1. Step 1: Trace the loop and effect of continue

    The loop runs from 1 to 5. When i equals 3, continue skips the addition for that iteration. Sum = 1 + 2 + 4 + 5 = 12 because 3 is skipped.
  2. Final Answer:

    12 because number 3 is skipped in addition -> Option B
  3. Quick Check:

    continue skips 3, sum = 12 [OK]
Hint: Continue skips current loop iteration [OK]
Common Mistakes:
  • Adding 3 despite continue
  • Thinking continue causes error
  • Summing only numbers less than 3
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