Learn how to use C++ break and continue statements to exit loops early, skip iterations, and control loop flow with clear beginner-friendly examples.
In C++, break and continue are control statements used inside loops to change the normal execution of a loop.
Normally, a loop executes all its statements repeatedly until its condition becomes false. However, sometimes we need to:
C++ provides two statements for these purposes:
break – Terminates the loop immediately.continue – Skips the remaining statements of the current iteration.The break statement is used to terminate a loop immediately.
When the break statement executes:
Syntax
break;
Task: Write a C++ program to print numbers from 1 to 10. Stop the loop when the number becomes 5.
Program
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
return 0;
}
Output
1
2
3
4
Explanation
i = 1.if statement checks whether i is equal to 5.i becomes 5, the break statement executes.| Iteration | Value of i | Condition i == 5 | Action |
|---|---|---|---|
| 1 | 1 | False | Print 1 |
| 2 | 2 | False | Print 2 |
| 3 | 3 | False | Print 3 |
| 4 | 4 | False | Print 4 |
| 5 | 5 | True | Exit loop |
Task: Write a program to print numbers from 1 to 10 using a while loop. Stop when the number becomes 6.
Program
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 10)
{
if (i == 6)
{
break;
}
cout << i << endl;
i++;
}
return 0;
}
Output
1
2
3
4
5
Explanation
The loop continues while i is less than or equal to 10. When i becomes 6, the break statement terminates the loop.
The continue statement is used to skip the remaining statements of the current iteration and move to the next iteration.
When the continue statement executes:
Syntax
continue;
Task: Write a C++ program to print numbers from 1 to 10, but skip number 5.
Program
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue;
}
cout << i << endl;
}
return 0;
}
Output
1
2
3
4
6
7
8
9
10
Explanation
i = 1.if statement checks whether i is equal to 5.i becomes 5, the continue statement executes.cout statement is skipped for that iteration.| Iteration | Value of i | Condition i == 5 | Action |
|---|---|---|---|
| 1 | 1 | False | Print 1 |
| 2 | 2 | False | Print 2 |
| 3 | 3 | False | Print 3 |
| 4 | 4 | False | Print 4 |
| 5 | 5 | True | Skip printing |
| 6 | 6 | False | Print 6 |
Task: Write a program to print odd numbers from 1 to 10 using a while loop.
Program
#include <iostream>
using namespace std;
int main()
{
int i = 0;
while (i < 10)
{
i++;
if (i % 2 == 0)
{
continue;
}
cout << i << endl;
}
return 0;
}
Output
1
3
5
7
9
Explanation
i starts from 0.i is increased by 1.i % 2 == 0 checks whether the number is even.continue skips the printing statement.i becomes 10.Important: In a while loop, make sure the loop-control variable is updated before executing continue when necessary. Otherwise, the loop may become an infinite loop.
break in Nested LoopsA break inside an inner loop terminates only the inner loop. The outer loop continues normally.
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 4; j++)
{
if (j == 3)
{
break;
}
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output:
1 2
1 2
1 2
The inner loop stops whenever j reaches 3. The outer loop still creates all three rows.
| break | continue |
|---|---|
| Terminates the loop immediately. | Skips the current iteration. |
| No further iterations are executed. | Remaining iterations continue normally. |
| Control moves outside the loop. | Control moves to the next iteration. |
| Used to stop a loop. | Used to skip selected iterations. |
break and continue are used inside loops.break terminates the nearest enclosing loop.continue skips the remaining statements of the current iteration.for, while, and do-while loops.break and continue affect only the innermost loop in which they are used.Predict the output of the following program.
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 7; i++)
{
if (i == 4)
{
break;
}
cout << i << " ";
}
return 0;
Modify the following program to skip all even numbers.
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
cout << i << " ";
}
return 0;
}
Write a C++ program that:
continue.break.Identify and correct the logical error in the following program.
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 10)
{
if (i == 5)
{
continue;
}
cout << i << " ";
i++;
}
return 0;
}
Solve Coding Exercises 32–33 in the Break and Continue section. Write, run, and test each C++ program to verify the output.