C++ break and continue
The while page ended with thirty-six lines on the screen, and the last one was 1, the final value that n printed in the countdown. This page starts from that screen, in the same file types.cpp, and the first box still holds the word pine. The for loop and the while loop stay in the file. The new work adds one more for loop after them. That loop uses continue and break so some passes print and some do not. The screen grows to thirty-nine lines, and the last three new lines are 0, then 1, then 3.
This page continues the Control Flow module. You already know how a loop repeats a block. Sometimes you need to skip the rest of one pass without ending the loop. Sometimes you need to leave the loop early, before the test would stop it on its own. Those two jobs are continue and break. They sit inside the body, usually under an if, and they change which lines reach the screen.
A plain for that prints every value from 0 through 4 would add five lines. This page’s for still walks k from 0 toward 5, but it skips the print when k is 2, and it leaves the loop when k is 4, before that print runs. The hole where 2 would have been, and the missing 4, are the teaching point. The switch cases above still end with break;. That break ends a switch. The new break on this page ends a loop. Same keyword, different statement around it.
The new loop starts with for (int k = 0; k < 5; k++). The counter is named k so it is not the same box as the earlier for index i or the while counter n. Inside the braces, the first check is if (k == 2) { continue; }. When that test is true, continue; skips the rest of the body for this pass and jumps to the step k++, then the for test runs again. The print line under that if does not run for k equal to 2.
The second check is if (k == 4) { break; }. When that test is true, break; leaves the whole for loop at once. The print line under that if does not run for k equal to 4, and k never reaches a later value inside this loop. When neither if matches, the body prints the current value of k with std::cout << k << std::endl;.
Walk the checked values. k starts at 0. Neither if matches, so the body prints 0. The step stores 1. Neither if matches, so the body prints 1. The step stores 2. The continue if matches, so the print is skipped and the step stores 3. Neither if matches, so the body prints 3. The step stores 4. The break if matches, so the loop ends. Three new lines appear: 0, then 1, then 3. The values 2 and 4 never print from this loop.
continue and break must stay inside a loop body. Writing either one outside a loop is a build error. Putting the print line above the if that runs continue would still print 2, because the print would run before the skip. On this page the continue if comes first, then the break if, then the print, so a skipped or broken pass does not write.
A common mistake is to confuse the two tools. continue only skips the rest of the current pass. The loop keeps going. break ends the loop. Nothing later in that for runs. Another common mistake is to put quotes around k on the print line, so the screen shows the letter k instead of the numbers. The print line in this file has no quotes around k.
The switch break lines above this loop stay as they were. They still end each case. They do not end the new for. Only the break; inside if (k == 4) ends the new for. Reading the braces tells you which statement owns which break.
This page stops before the next loop shape that walks every element of a list without a hand-written index. The next page names that tool once at the end.
Type the file in this order, or add only the new lines if types.cpp is already on disk. After the while loop that prints n, and before return 0;, add the for loop with k, the continue if, the break if, and the print line. The file name on disk stays types.cpp. The checked file is below, and the new lines are that for statement and the ifs inside its braces.
#include <iostream>
#include <string>
int main()
{
// the word stays pine
std::string word = "pine";
int count = 2;
double ratio = 0.5;
bool ready = true;
double price = 7.9;
const int limit = 10;
int scores[3] = {4, 5, 6};
std::string other = "cone";
std::string joined = word + " " + other;
int total = 17;
int step = 3;
step += 4;
int score = 72;
int goal = 50;
bool wet = false;
std::string result;
int day = 3;
std::string name;
std::cout << word << std::endl;
std::cout << count << std::endl;
std::cout << ratio << std::endl;
std::cout << ready << std::endl;
std::cout << word.size() << std::endl;
std::cout << joined << std::endl;
std::cout << word[0] << std::endl;
std::cout << joined.find("cone") << std::endl;
std::cout << joined.substr(5, 4) << std::endl;
std::cout << count * 4 << std::endl;
std::cout << 7 / 2 << std::endl;
std::cout << 7.0 / 2 << std::endl;
std::cout << (count > 1) << std::endl;
std::cout << (count == 3) << std::endl;
std::cout << !ready << std::endl;
std::cout << static_cast<int>(price) << std::endl;
std::cout << static_cast<double>(count) / 4 << std::endl;
std::cout << static_cast<int>(ratio * 3) << std::endl;
std::cout << limit << std::endl;
std::cout << limit * count << std::endl;
std::cout << scores[0] << std::endl;
std::cout << scores[2] << std::endl;
std::cout << total % 5 << std::endl;
std::cout << step << std::endl;
std::cout << (score >= goal) << std::endl;
std::cout << (score < goal) << std::endl;
std::cout << ((score >= goal) && (count == 2)) << std::endl;
std::cout << ((score < goal) || wet) << std::endl;
if (score >= goal) {
result = "pass";
} else {
result = "retry";
}
std::cout << result << std::endl;
if (wet) {
std::cout << "wet" << std::endl;
}
switch (day) {
case 1:
name = "mon";
break;
case 2:
name = "tue";
break;
case 3:
name = "wed";
break;
default:
name = "other";
break;
}
std::cout << name << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << i << std::endl;
}
int n = 3;
while (n > 0) {
std::cout << n << std::endl;
n--;
}
for (int k = 0; k < 5; k++) {
if (k == 2) {
continue;
}
if (k == 4) {
break;
}
std::cout << k << std::endl;
}
return 0;
}
Rebuild with g++ -std=c++17 types.cpp -o types, then run ./types. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 types.cpp -o types. The screen shows thirty-nine lines. The first thirty-six are the same as on the while page. The last three are 0, then 1, then 3, the values that this new for printed.
Check the last three lines first. The loop printed 0 and 1, skipped 2 with continue, printed 3, and left at 4 with break. The table gives each row its piece, so compare your run with it line by line. If the screen shows five new lines 0 through 4, both early tools are missing. If 2 appears, the continue if is missing or sits after the print. If 4 appears, the break if is missing or sits after the print. If the last three lines are the letter k, the print line has quotes around k.
📊 The break and continue loop and what it shows
| Piece | What the screen shows |
|---|---|
| First | 0, because k starts at 0 and the first pass prints that value |
| Last | 3, because continue skips 2 and break stops before 4 prints |
| Rule | continue skips the rest of one pass; break leaves the whole loop |
| Screen | pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, 3.5, 1, 0, 0, 7, 0.5, 1, 10, 20, 4, 6, 2, 7, 1, 0, 1, 0, pass, wed, 0, 1, 2, 3, 2, 1, 0, 1, 3 |
Try one change at a time. Change if (k == 2) to if (k == 1), leave the break at 4, and rebuild. The new lines become 0, then 3 only, because continue now skips 1 and break still stops before 4. Put k == 2 back. Next, delete only the break if block, leave the continue at 2, and rebuild. The new lines become 0, then 1, then 3, then 4, because 2 is still skipped and the loop now runs through 4. Put the break if back. Each experiment changes one thing, so the screen shows exactly which early tool caused the difference. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the early-exit tools and stops before the next loop shape. The next page is range-for, which walks each element of an array without writing the index by hand. The break and continue you have just written are the right tools when one pass must stop early or skip its print. You leave with thirty-nine lines on the screen, and the last one is 3. Count the lines before you leave. There are thirty-nine, and the last one is 3.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top