C++ Tutorial
C++ Break and Continue
break leaves a loop early. continue skips the rest of this iteration and starts the next.
Stop, or skip one pass
Loops run until their condition is false. Sometimes you want out sooner: you found the value, the input is bad, the count is high enough. break leaves the loop immediately. The next statement after the loop runs next.
continue does not leave the loop. It skips the rest of the current pass and goes back to the condition (for while) or to the step and then the condition (for for).
break leaves the loop
This program prints 1, 2, 3 and then stops. When n is 3, break jumps out. 4 and 5 never print, even though the while condition would have allowed them.
Example
#include <iostream>
using namespace std;
int main() {
int n = 1;
while (n <= 5) {
cout << n << endl;
if (n == 3) {
break;
}
n = n + 1;
}
cout << "after the loop" << endl;
return 0;
}Compile in /cpp/try. The line after the loop still runs. break ends the loop, not the whole program.
continue skips this iteration
This loop prints the odd numbers from 1 to 5. When n is even, continue jumps to the next pass. The cout below it does not run for those values.
Example
#include <iostream>
using namespace std;
int main() {
for (int n = 1; n <= 5; n = n + 1) {
if (n % 2 == 0) {
continue;
}
cout << n << endl;
}
return 0;
}% is remainder. Even numbers have remainder 0 when divided by 2, so they are skipped.
continue and while
In a for loop the step still runs after continue. In a while loop there is no hidden step. If the update is below continue, that update never happens and you can loop forever.
Example
#include <iostream>
using namespace std;
int main() {
int n = 0;
while (n < 5) {
n = n + 1;
if (n == 3) {
continue;
}
cout << n << endl;
}
return 0;
}Increment n before continue in a while loop. Put the update after continue and n == 3 never changes.
break in switch is different
You already used break inside switch to stop fall-through. Same keyword, different job: it leaves the switch, not a loop. A break inside a switch that sits inside a loop does not end the loop. It only ends the switch.
continue is not used with switch. It only makes sense in loops.
When to use which
| Keyword | Effect | Typical use |
|---|---|---|
break | Leave the loop now | Found a match, stop searching |
continue | Skip the rest of this pass | Ignore one value, keep looping |
Prefer a clear condition when it reads well: while (n <= 5 && !found) can replace a break. Use break when the stop is in the middle of the body and rewriting the condition would be messier.
Nested loops
break and continue apply to the innermost loop that contains them. Breaking an inner loop does not leave the outer one. If you need to stop both, set a flag in the inner loop and test it in the outer, or put the work in a function and return.
Next: arrays — a fixed list of values, indexed from 0, which for walks naturally.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Physics
A pressure trip
A relief valve is supposed to open before the vessel yields. If readings climb 101, 140, 180, 250 kPa and the limit is 200, you stop at 250 — the first value over the line.
break leaves the loop. The remaining samples are unread on purpose.
Example
#include <iostream>
using namespace std;
int main() {
int kPa[] = {101, 140, 180, 250, 310};
for (int i = 0; i < 5; i++) {
if (kPa[i] > 200) {
cout << "stop at " << kPa[i] << " kPa" << endl;
break;
}
cout << "ok " << kPa[i] << " kPa" << endl;
}
return 0;
}Maths
Odds only
The sum of odd numbers 1 + 3 + … + 9 is 25, which is also 5². continue skips the even n so they never reach the accumulator.
Example
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int n = 1; n <= 9; n++) {
if (n % 2 == 0) {
continue;
}
sum += n;
}
cout << "odd sum = " << sum << endl;
return 0;
}