0
0
C++programming~10 mins

Break statement in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Break statement
Start Loop
Check Condition
Yes
Execute Loop Body
Is break condition met?
YesExit Loop
No
Continue Loop
Back to Check Condition
The loop starts and checks its condition. Inside the loop, if the break condition is met, the loop stops immediately. Otherwise, it continues looping.
Execution Sample
C++
for (int i = 0; i < 5; i++) {
    if (i == 3) {
        break;
    }
    std::cout << i << " ";
}
This code prints numbers from 0 up to 2, then stops the loop when i equals 3.
Execution Table
IterationiCondition (i < 5)Break Condition (i == 3)ActionOutput
10TrueFalsePrint 00
21TrueFalsePrint 11
32TrueFalsePrint 22
43TrueTrueBreak loop
5-Loop exited---
💡 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 numbers after 2 even though the loop condition allows i < 5?
Because at iteration 4 when i equals 3, the break condition is True, so the loop stops immediately as shown in execution_table row 4.
Does the break statement skip the current iteration or stop the entire loop?
The break statement stops the entire loop immediately, not just skipping the current iteration, as seen in execution_table row 4 where the loop exits.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i when the break statement is executed?
A3
B2
C4
D5
💡 Hint
Check the 'Break Condition' and 'i' columns in execution_table row 4.
At which iteration does the loop stop running according to the execution table?
AIteration 5
BIteration 3
CIteration 4
DIteration 6
💡 Hint
Look at the 'Action' column where the break occurs in execution_table row 4.
If the break condition was changed to (i == 2), what would be the last number printed?
A2
B1
C3
D4
💡 Hint
Refer to variable_tracker and execution_table logic for when break triggers.
Concept Snapshot
Break statement in C++:
- Used inside loops to stop the loop immediately.
- Syntax: if (condition) break;
- When break runs, loop exits regardless of loop condition.
- Useful to stop loops early based on a condition.
- After break, code continues after the loop.
Full Transcript
This example shows a for loop counting from 0 to 4. Inside the loop, there is a check if i equals 3. When i is 3, the break statement runs, which stops the loop immediately. Before that, the loop prints the values 0, 1, and 2. The execution table shows each iteration, the value of i, whether the loop condition and break condition are true, the action taken, and the output. The variable tracker shows how i changes each iteration. Key moments explain why the loop stops early and how break works. The quiz questions test understanding of when break triggers and how it affects output.