We will study the break
and continue
statement and how it functions in loops in this tutorial with the aid of examples.
C++ Break Statement
We will study the break
statement and how it functions in loops in this tutorial with the aid of examples.
You can exit a loop by using the break statement.
When i equals 6, this example exits the loop:
#include <iostream> using namespace std; int main() { for (int i = 0; i < 10; i++) { if (i == 6) { break; } cout << i << "\n"; } return 0; }
OUTPUT:
0
1
2
3
4
5
C++ Continue Statement
If one iteration of the loop is broken by the continue statement due to the occurrence of a specified condition, the loop then moves on to the next iteration.
Syntax:
continue;
Example: continue
with for
loop
Continue in a for
loop skips the current iteration and jumps to the update expression instead.
// program to print the value of i //geektocode.com #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { // condition to continue if (i == 3) { continue; } cout << i << endl; } return 0; }
OUTPUT:
1
2
4
5