-
Hajipur, Bihar, 844101
In C++, loops are powerful tools for repeating tasks. But sometimes, you need to stop a loop early or skip certain iterations. This is where break and continue statements come in. They give you precise control over loop execution and make your programs more efficient and logical.
The break statement is used to terminate a loop immediately.
When the program encounters a break, it stops executing the loop and jumps to the first statement after the loop.
It works in all loop types – for, while, and do-while – and also in switch statements.
break;
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 6)
break;
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4 5
In this program, when i becomes 6, the break statement stops the loop completely, and control moves out of the loop block.
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 10) {
if (i == 4)
break;
cout << i << " ";
i++;
}
return 0;
}
Output:
1 2 3
The break here exits the loop when i equals 4, ignoring the rest of the iterations.
You can use the break statement:
To stop a loop when a certain condition is met.
To end an infinite loop.
To exit early when a required element is found in a list or array.
Example: Search in an Array
#include <iostream>
using namespace std;
int main() {
int arr[5] = {2, 4, 6, 8, 10};
int num = 8;
bool found = false;
for (int i = 0; i < 5; i++) {
if (arr[i] == num) {
found = true;
break;
}
}
if (found)
cout << "Number found!";
else
cout << "Number not found!";
return 0;
}
Output:
Number found!
Here, the loop stops immediately after finding the number, saving unnecessary iterations.
The continue statement is used to skip the current iteration of a loop and jump directly to the next one.
Unlike break, it doesn’t stop the loop — it just skips the rest of the code for the current iteration.
continue;
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0)
continue;
cout << i << " ";
}
return 0;
}
Output:
1 3 5 7 9
In this program, continue skips the even numbers by jumping to the next iteration when i is divisible by 2.
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 10) {
i++;
if (i == 5)
continue;
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4 6 7 8 9 10
Here, the loop skips printing the number 5 because the continue statement bypasses that iteration.
| Feature | break | continue |
|---|---|---|
| Function | Exits the loop completely | Skips the current iteration |
| Control Flow | Moves control to the next statement after the loop | Moves control to the loop’s condition or update part |
| Usage | When you want to stop looping | When you want to skip specific values |
| Example | Exit when a condition is met | Skip certain conditions |
When you use break inside nested loops, it only stops the inner loop where it is written.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2)
break;
cout << "i=" << i << ", j=" << j << endl;
}
}
return 0;
}
Output:
i=1, j=1
i=2, j=1
i=3, j=1
As soon as j equals 2, the inner loop breaks, but the outer loop continues.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2)
continue;
cout << "i=" << i << ", j=" << j << endl;
}
}
return 0;
}
Output:
i=1, j=1
i=1, j=3
i=2, j=1
i=2, j=3
i=3, j=1
i=3, j=3
The continue skips printing the pair where j equals 2.
#include <iostream>
using namespace std;
int main() {
int num;
while (true) {
cout << "Enter a number (0 to stop): ";
cin >> num;
if (num == 0)
break;
cout << "You entered: " << num << endl;
}
cout << "Loop ended.";
return 0;
}
Output:
Enter a number (0 to stop): 5
You entered: 5
Enter a number (0 to stop): 9
You entered: 9
Enter a number (0 to stop): 0
Loop ended.
Here, the infinite loop runs until the user enters 0, and break stops it safely.
Using them outside loops or switch blocks – This causes a compilation error.
Placing code after continue inside loops – Any code after continue in the same block is never executed.
Using break in nested loops without planning – It only exits the innermost loop.
The break and continue statements in C++ give developers control over loop behavior.
Use break to exit a loop early when a specific condition is met.
Use continue to skip unwanted iterations and move to the next one.
They make code cleaner, prevent unnecessary computation, and handle specific scenarios efficiently.
Both are essential tools when writing optimized and structured loops in C++.
Write a C++ program to display numbers from 1 to 50 but stop printing when the number reaches 25 using a break statement.
Write a program that reads 10 integers from the user and stops input if a negative number is entered using break.
Create a program to search for a specific element in an array and stop searching once it’s found using break.
Write a C++ program to print numbers from 1 to 20 but skip all even numbers using a continue statement.
Write a program that reads numbers from the user until the number 0 is entered. Use break to stop the loop.
Write a C++ program that prints only multiples of 3 from 1 to 30 using continue.
Create a program that prints numbers from 1 to 10 but stops printing if the current number is greater than 7 using break.
Write a C++ program that takes 10 numbers as input but skips any number that is negative using continue.
Write a program using nested loops that prints a multiplication table but stops the inner loop when the result exceeds 20 using break.
Write a C++ program that continuously asks for a password and breaks the loop once the correct password is entered.