In this tutorial let us discuss the C++break statements and C++ continue statements in detail with suitable examples.
Before learning about the break statement and continue statement in C++ make sure that you are familiar with:
In C++ Language among the 32 keywords, break
is the most commonly used keyword along with the loop. A break statement forms when the keyword break is followed by a semicolon. The breaks statement is used to break the loop execution series immediately. The prototype is as follows:
When the break
statement is encountered in C++, the loop is terminated.
The break
statement has the following syntax:
break;
The break statement is mostly used with if statements in loops and switch cases. Initially, we can chase how the break statement works in loops to break the loop control.
Now let us discuss the working of break statements in different types of loops.
To make it more clear see below code snippet and observe how for loop control terminates while encountering break statement.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 7; i++) {
// break condition
if (i == 4) {
break;
}
cout << i << endl;
}
return 0;
}
Output:
1 2 3
The for loop is used in the preceding program to print the value of i after each iteration. Take note of the code:
if (i == 4)
{
break;
}
This means that when the i is equal to 4, then the break statement ends the loop. As a result, the output does not include any values which is greater than or equal to 3.
let us write a program to calculate the sum of positive numbers and suppose if the user enters a negative number, then the loop is broken and when the negative number is entered it is not added to the sum.
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
while (true) {
// take input from the user
cout << " Please enter a number: ";
cin >> number;
// break condition
if (number < 0) {
break;
}
// add all positive numbers
sum += number;
}
// display the sum
cout << "The sum is " << sum << endl;
return 0;
}
Output:
Please enter a number: 5 Please enter a number: 10 Please enter a number: 20 Please enter a number: 1 Please enter a number: 4 Please enter a number: -8 The sum is 40
The user enters a number into the above program. Then the while loop is used to print the total sum of the numbers that the user has entered. Take note of the code here:
if (number < 0) {
break;
}
When the user enters a negative number, the break statement terminates the loop and code outside the loop is executed.
The while loop will continue indefinitely unless the user enters a negative number.
Break terminates the inner loop when used with nested loops.
As an example,
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
// nested for loops
// first loop
for (int i = 1; i <= 4; i++) {
// second loop
for (int j = 1; j <= 4; j++) {
if (i == 3) {
break;
}
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
Output:
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 1, j = 4 i = 2, j = 1 i = 2, j = 2 i = 2, j = 3 i = 2, j = 4 i = 4, j = 1 i = 4, j = 2 i = 4, j = 3 i = 4, j = 4
When i == 3, the break statement is executed in the preceding program. It ends the inner loop and shifts the program's control flow to the outer loop.
As a result, the value i= 3 is never displayed in the output.
The switch statement and the break statement are both used together. More information can be found in the C++ switch statement tutorial.
The continue statement is used in computer programming to skip the current iteration of the loop and move the control of a particular program to the next iteration.
The continue statement has the following syntax:
continue;
continue in a for loop skips the current iteration and jumps to the update expression.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 4; i++) {
// condition to continue
if (i == 2) {
continue;
}
cout << i << endl;
}
return 0;
}
Output:
1 3 4
continue in a while loop will skip the current iteration and returns the program's control flow to the while condition.
Now let us write a program in order to calculate positive numbers till 30 onlyand suppose if the user is entering a number that is negative then that particular number will be skipped from the calculation. .
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int number = 0;
while (number >= 0) {
// add all positive numbers
sum += number;
// take input from the user
cout << "Enter a number: ";
cin >> number;
// continue condition
if (number > 30) {
cout << "The number is greater than 30 and won't be calculated." << endl;
number = 0; // the value of number is made 0 again
continue;
}
}
// display the sum
cout << "The sum is " << sum << endl;
return 0;
}
Output:
Output 1 Enter a number: 14 Enter a number: 8 Enter a number: -9 The sum is 22 Output 2 Enter a number: 12 Enter a number: 0 Enter a number: 7 Enter a number: 89 The number is greater than 30 and won't be calculated. Enter a number: -3 The sum is 19
The user enters a number into the above program. The while loop is used to print the total sum of positive numbers which is entered by the user that are not greater than 30.
Take note of the continue statement.
if (number > 30) {
continue;
}
Suppose, when a number greater than 30 is entered by the user, the continue statement will skip the current iteration. The program's control flow then moves to the while loop condition.
The loop ends when the user enters a number less than zero.
Continue will skip the current iteration of the inner loop when used with nested loops. As an example,
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
// nested for loops
// first loop
for (int i = 1; i <= 3; i++) {
// second loop
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
When the continue statement in the preceding program is executed, the current iteration in the inner loop is skipped. And the program's control shifts to the inner loop's update expression.
As a result, the value j = 2 has never been displayed in the output.
Point to remember
The break
statement always ends the loop completely. The continue
statement, on the other hand, only skips the current iteration.