The loop runs checking a condition each time. If the break statement runs, it immediately exits the loop, skipping remaining iterations.
Execution Sample
Python
for i inrange(5):
if i == 3:
breakprint(i)
This code prints numbers from 0 to 4 but stops early when i equals 3.
Execution Table
Iteration
i value
Condition i==3?
Break triggered?
Action
Output
1
0
False
No
Print 0
0
2
1
False
No
Print 1
1
3
2
False
No
Print 2
2
4
3
True
Yes
Break loop
-
-
-
-
Loop ends early
-
💡 Loop stops when i == 3 because break is triggered.
Variable Tracker
Variable
Start
After 1
After 2
After 3
Final
i
-
0
1
2
3
Key Moments - 3 Insights
Why does the loop stop printing after i reaches 3?
Because at iteration 4 (i=3), the condition i==3 is True, so the break statement runs and immediately exits the loop, as shown in execution_table row 4.
Does the break statement skip just the current iteration or the whole loop?
The break statement exits the entire loop immediately, not just skipping the current iteration. This is clear from the execution_table where after break, no further iterations happen.
What happens if the break condition never becomes True?
If break never triggers, the loop runs all iterations normally, printing all values. This is implied by the flow and would show all iterations without break in the table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i when the break is triggered?
A2
B4
C3
D0
💡 Hint
Check the 'i value' column in the row where 'Break triggered?' is Yes.
At which iteration does the loop stop running according to the execution_table?
AIteration 4
BIteration 3
CIteration 5
DIteration 2
💡 Hint
Look at the iteration number where 'Break triggered?' is Yes.
If the break condition was changed to i == 10, what would happen to the output?
AThe loop would print nothing
BThe loop would print 0 to 4
CThe loop would break immediately
DThe loop would print only 0
💡 Hint
Refer to variable_tracker and execution_table to see that break triggers only when condition is True.
Concept Snapshot
Break statement behavior in loops:
- Syntax: break
- When break runs, loop exits immediately
- Skips remaining iterations
- Useful to stop loop early based on condition
- If break never runs, loop completes all iterations
Full Transcript
This visual execution shows how the break statement works inside a Python for loop. The loop runs from i=0 to i=4. Each iteration checks if i equals 3. When i is 3, the break statement triggers and immediately exits the loop. This stops any further printing or looping. The variable tracker shows how i changes each iteration. Key moments clarify that break exits the whole loop, not just one iteration. The quiz questions help check understanding of when and why break stops the loop.