Control Flow
Break and Continue
continue skips to the next loop pass, while break stops the loop.
break
`break` exits the nearest loop immediately.
Break and Continue
break_continue.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
int main() {
int stopAt = 8;
int values[6] = {2, -1, 4, 6, -3, 8};
int total = 0;
for (int value : values) {
if (value < 0) {
continue;
}
if (value == stopAt) {
break;
}
total += value;
}
std::cout << "stopAt=" << stopAt << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
int main() {
int stopAt = 6;
int values[6] = {2, -1, 4, 6, -3, 8};
int total = 0;
for (int value : values) {
if (value < 0) {
continue;
}
if (value == stopAt) {
break;
}
total += value;
}
std::cout << "stopAt=" << stopAt << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
int main() {
int stopAt = 10;
int values[6] = {2, -1, 4, 6, -3, 8};
int total = 0;
for (int value : values) {
if (value < 0) {
continue;
}
if (value == stopAt) {
break;
}
total += value;
}
std::cout << "stopAt=" << stopAt << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
stopAt ← 8, values ← ⟨addr A⟩, total ← 0
3int main() {4 int stopAt→ 8 = 8; //@stopAt=6, 105 int values→ ⟨addr A⟩[6] = {2, -1, 4, 6, -3, 8};6 int total→ 0 = 0;total ← 2
pass 1 of 68for (int value2 : values⟨addr A⟩) {9 if (value < 0) {10 continue;11 }12 if (value == stopAt) {13 break;14 }15 total→ 2 += value2;16}All 6 passes — pass 1 is the card above pass valuestopAttotal1 2 — 0 → 2 2 -1 — — 3 4 — 2 → 6 4 6 — 6 → 12 5 -3 — — 6 8 8 — if (value < 0)
pass 1 of 28for (int value : values) {9 if (value-1 < 0) {10 continue;11 }if (value < 0)
pass 2 of 28for (int value : values) {9 if (value-3 < 0) {10 continue;11 }if (value == stopAt)
11}12if (value8 == stopAt8) {13 break;14}std::cout << "stopAt=" << stopAt << std::endl;
18 std::cout << "stopAt=" << stopAt8 << std::endl;19 std::cout << "total=" << total12 << std::endl;20 return 0;21}outputstopAt=8 total=12
stopAt ← 6, values ← ⟨addr A⟩, total ← 0
3int main() {4 int stopAt→ 6 = 6;5 int values→ ⟨addr A⟩[6] = {2, -1, 4, 6, -3, 8};6 int total→ 0 = 0;total ← 2
pass 1 of 48for (int value2 : values⟨addr A⟩) {9 if (value < 0) {10 continue;11 }12 if (value == stopAt) {13 break;14 }15 total→ 2 += value2;16}All 4 passes — pass 1 is the card above pass valuestopAttotal1 2 — 0 → 2 2 -1 — — 3 4 — 2 → 6 4 6 6 — if (value < 0)
8for (int value : values) {9 if (value-1 < 0) {10 continue;11 }if (value == stopAt)
11}12if (value6 == stopAt6) {13 break;14}std::cout << "stopAt=" << stopAt << std::endl;
18 std::cout << "stopAt=" << stopAt6 << std::endl;19 std::cout << "total=" << total6 << std::endl;20 return 0;21}outputstopAt=6 total=6
stopAt ← 10, values ← ⟨addr A⟩, total ← 0
3int main() {4 int stopAt→ 10 = 10;5 int values→ ⟨addr A⟩[6] = {2, -1, 4, 6, -3, 8};6 int total→ 0 = 0;total ← 2
pass 1 of 68for (int value2 : values⟨addr A⟩) {9 if (value < 0) {10 continue;11 }12 if (value == stopAt) {13 break;14 }15 total→ 2 += value2;16}All 6 passes — pass 1 is the card above pass valuetotal1 2 0 → 2 2 -1 — 3 4 2 → 6 4 6 6 → 12 5 -3 — 6 8 12 → 20 if (value < 0)
pass 1 of 28for (int value : values) {9 if (value-1 < 0) {10 continue;11 }if (value < 0)
pass 2 of 28for (int value : values) {9 if (value-3 < 0) {10 continue;11 }std::cout << "stopAt=" << stopAt << std::endl;
18 std::cout << "stopAt=" << stopAt10 << std::endl;19 std::cout << "total=" << total20 << std::endl;20 return 0;21}outputstopAt=10 total=20
Skip Negatives, Stop at Match
- The loop visits
2,-1,4,6,-3, and8. - Negative values use
continue, so they are skipped. - When the loop reaches
8,breakstops before adding it. - The total is
2 + 4 + 6, which is12. | Value | Action | Running total | | --- | --- | --- | |2| add |2| |-1| continue |2| |4| add |6| |6| add |12| |-3| continue |12| |8| break |12|
Exercise: break_continue.cpp
Skip negative values with continue, stop at a target with break, and print the total