Bird
Raised Fist0
Javaprogramming~10 mins

While loop execution flow in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - While loop execution flow
Initialize variable
↓
Check condition
Yes↓
Execute loop body
↓
Update variable
Back to Check↓
Exit loop
↩Back to Check
The while loop starts by checking a condition. If true, it runs the loop body, updates variables, then checks again. It stops when the condition is false.
Execution Sample
Java
int i = 0;
while (i < 3) {
    System.out.println(i);
    i++;
}
Prints numbers 0, 1, 2 by looping while i is less than 3.
Execution Table
Stepi valueCondition (i < 3)ActionOutput
10truePrint 0; i = i + 10
21truePrint 1; i = i + 11
32truePrint 2; i = i + 12
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, so the loop exits as shown in the execution_table row 4.
When is the variable i updated in the loop?
i is updated after printing in each loop iteration, as shown in the Action column of execution_table rows 1 to 3.
Does the loop body run if the condition is false at the start?
No, the condition is checked before the loop body runs. If false initially, the loop body never executes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i at step 2?
A1
B0
C2
D3
💡 Hint
Check the 'i value' column in execution_table row 2.
At which step does the condition become false and the loop stops?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'Condition' column in execution_table to find when it is false.
If we change the initial i to 3, what happens to the loop execution?
ALoop runs once
BLoop does not run at all
CLoop runs three times
DLoop runs infinitely
💡 Hint
Recall that the condition is checked before the loop body runs, see key_moments about initial condition.
Concept Snapshot
while (condition) {
    // code runs while condition is true
    // update variables inside to avoid infinite loop
}

- Condition checked before each loop
- Loop stops when condition is false
- Variables usually updated inside loop
Full Transcript
This visual shows how a while loop works in Java. First, a variable i is set to 0. Then the loop checks if i is less than 3. If yes, it prints i and adds 1 to i. 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, the condition result, the action taken, and the output printed. The variable tracker shows how i changes after each loop. 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 what happens if i starts at 3.

Practice

(1/5)
1. What does a while loop do in Java?
easy
A. Stops the program immediately
B. Executes code only once regardless of condition
C. Runs code only if a condition is false
D. Repeats a block of code as long as a condition is true

Solution

  1. Step 1: Understand the purpose of a while loop

    A while loop repeats its code block while the condition remains true.
  2. Step 2: Compare options with this behavior

    Only Repeats a block of code as long as a condition is true correctly describes this repeating behavior based on a true condition.
  3. Final Answer:

    Repeats a block of code as long as a condition is true -> Option D
  4. Quick Check:

    while loop repeats while condition true = A [OK]
Hint: Remember: while loops run only when condition is true [OK]
Common Mistakes:
  • Thinking while loop runs once regardless of condition
  • Confusing while with if statement
  • Believing while loop runs when condition is false
2. Which of the following is the correct syntax to start a while loop in Java?
easy
A. while condition { }
B. while (condition) { }
C. while condition: { }
D. while (condition);

Solution

  1. Step 1: Recall Java while loop syntax

    Java requires parentheses around the condition and curly braces for the loop body.
  2. Step 2: Check each option

    while (condition) { } matches the correct syntax: while (condition) { }.
  3. Final Answer:

    while (condition) { } -> Option B
  4. Quick Check:

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

Solution

  1. Step 1: Analyze the loop condition and variable changes

    Variable i starts at 1 and increases by 1 each loop until i > 3.
  2. Step 2: Trace the output for each iteration

    i=1 prints "1 ", i=2 prints "2 ", i=3 prints "3 ". Loop stops after i=4.
  3. Final Answer:

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

    Prints 1 to 3 with spaces = D [OK]
Hint: Check loop start, end condition, and increments carefully [OK]
Common Mistakes:
  • Forgetting i++ causes infinite loop
  • Printing extra number after loop ends
  • Starting i from 0 instead of 1
4. Identify the error in this code snippet:
int count = 5;
while (count > 0) {
  System.out.println(count);
}
medium
A. No update to count inside the loop causing infinite loop
B. Missing semicolon after while condition
C. Variable count is not initialized
D. Incorrect print statement syntax

Solution

  1. Step 1: Check loop condition and variable updates

    The loop condition is count > 0, but count is never changed inside the loop.
  2. Step 2: Understand effect of no update

    Without changing count, the loop runs forever, causing an infinite loop.
  3. Final Answer:

    No update to count inside the loop causing infinite loop -> Option A
  4. Quick Check:

    Loop variable must change to stop loop = B [OK]
Hint: Always update loop variable inside while loop to avoid infinite loops [OK]
Common Mistakes:
  • Assuming loop stops without variable update
  • Confusing syntax errors with logic errors
  • Ignoring infinite loop risks
5. How many times will the loop run in this code?
int x = 10;
while (x > 0) {
  x -= 3;
  System.out.print(x + " ");
}
hard
A. 3 times
B. 5 times
C. 4 times
D. Infinite times

Solution

  1. Step 1: Trace the value of x each iteration

    Start x=10. After each loop: x=7, x=4, x=1, x=-2 (loop stops after this).
  2. Step 2: Count how many times loop runs

    Loop runs while x > 0, so it runs 3 times before x becomes -2.
  3. Final Answer:

    3 times -> Option A
  4. Quick Check:

    Subtract 3 until x ≤ 0 = 3 times [OK]
Hint: Count iterations by subtracting until condition fails [OK]
Common Mistakes:
  • Counting one less or more iteration
  • Ignoring last iteration where x becomes negative
  • Assuming infinite loop without checking condition
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