0
0
Javaprogramming~10 mins

Break statement in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Break statement
Start Loop
Check Condition
Yes
Execute Loop Body
Is break triggered?
YesExit Loop
No
Next Iteration
Back to Check Condition
End Loop
Back to Check Condition
The loop starts and checks its condition. If true, it runs the body. If a break is triggered inside, it exits the loop immediately. Otherwise, it continues to the next iteration until the condition is false.
Execution Sample
Java
for (int i = 0; i < 5; i++) {
    if (i == 3) {
        break;
    }
    System.out.println(i);
}
This loop prints numbers from 0 to 4 but stops early when i equals 3.
Execution Table
Iterationi valueCondition (i < 5)Break Condition (i == 3)ActionOutput
10truefalsePrint 00
21truefalsePrint 11
32truefalsePrint 22
43truetrueBreak loop
--false-Exit loop-
💡 Loop exits at iteration 4 because break condition i == 3 is true.
Variable Tracker
VariableStartAfter 1After 2After 3Final
i01233
Key Moments - 2 Insights
Why does the loop stop printing when i reaches 3?
Because at iteration 4, the break condition (i == 3) becomes true, triggering the break statement which immediately exits the loop (see execution_table row 4).
Does the loop condition (i < 5) stop the loop in this example?
No, the loop condition is still true when i is 3, but the break statement stops the loop early before the next iteration (see execution_table rows 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i when the break statement is triggered?
A2
B4
C3
D5
💡 Hint
Check the 'Break Condition' and 'i value' columns at iteration 4 in the execution_table.
At which iteration does the loop stop executing the print statement?
AIteration 4
BIteration 3
CIteration 5
DIteration 2
💡 Hint
Look at the 'Action' and 'Output' columns in the execution_table to see when printing stops.
If the break statement was removed, what would be the last value printed?
A3
B4
C5
D2
💡 Hint
Without break, the loop runs until i < 5 is false, so check the maximum i value before condition fails.
Concept Snapshot
Break statement in Java:
- Used inside loops to exit immediately.
- Syntax: break;
- Stops loop even if condition is still true.
- Useful to stop early based on a condition.
- After break, code continues after the loop.
Full Transcript
This example shows a for loop in Java that counts from 0 to 4. Inside the loop, there is a check if the variable i equals 3. When this happens, the break statement runs, which immediately stops the loop. The loop prints the values 0, 1, and 2, then stops before printing 3 because of the break. The loop condition i < 5 remains true at that point, but break overrides it to exit early. This helps stop loops when a certain condition is met without waiting for the loop to finish all iterations.