Break and continue
To alter the normal flow of the looping constructs, i.e. change their behavior, we can use two important keywords: break
and continue
. They both work for all types of loops.
break
statement​
This instruction (just like for switch statements) is used to jump out of a loop. break
terminates the loop when it is encountered.
Example 1​
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
cout << i << endl;
}
0
1
2
Example 2​
note
break
commands are usually used in conjunction with if statements and are the only way to stop an infinite loop (except for closing the program while it's running).
int x = 0;
while (true) {
cout << x << endl;
if (x > 100) {
break;
}
x++;
}
continue
statement​
The continue
statement is used to skip the current iteration of the loop and go to the next iteration. Unlike the break
statement, continue
doesn't skip all the remaining iterations of the loop but just one.
Example​
for (int i = 0; i < 10; i++) {
if (i > 4) {
continue;
}
// when i > 4 this instruction is no longer executed
cout << i << "\n";
}
0
1
2
3
4