14
SepContinue statement in C++: Difference between break and continue statement
21 May 2024
Beginner
1.57K Views
13 min read
Continue Statement in C++: An Overview
In this C++ tutorial on jump statements, we will now look at thecontinue
statement in C++ in detail. We saw that the break statement is used to prematurely exit from the loop or switch case. The continue
statement works a little differently than the break
statement. For a more in-depth understanding, consider our C++ Certification program.What is the Continue Statement in C++?
Thecontinue
statement is used to skip the current iteration of any loop
and bring the control to the beginning of the iteration. The statements following the continue statement are skipped and the iteration starts again.Syntax
//loop statements
continue;
//some lines of the code which is to be skipped
We will now see the continue
statement with the for loop, while loop, do...while loop, and nested loop in C++. If you are weak in the concepts of loops
and conditional statements
, go back and refer to the tutorials on conditional statements in C++ and loops in C++ programming.
Read More - C++ Interview Interview Questions and Answers
Continue Statement with for loop in C++
In afor loop
, the continue
statement skips the current iteration and the control goes to the update expression
.Example to demonstrate continue statement with for loop in C++ Compiler
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
cout << i << endl;
}
return 0;
}
- This C++ program iterates 10 times using a
for loop
. - The
continue
statement is encountered wheni=5
. So it skips the remainder of the loop's body for that iteration. - As a result, it only prints the numbers 1-4 and 6-10, skipping 5.
Output
1
2
3
4
6
7
8
9
10
Continue Statement with while loop in C++
In awhile loop
, the continue
statement skips the current iteration and the control goes back to the while condition
.Example to demonstrate continue statement with while loop in C++
#include <iostream>
using namespace std;
int main()
{
int i = 0;
while (i < 10) {
i++;
if (i == 5) {
continue; //the iteration is skipped
}
else {
// prints the value of i
cout << i << " ";
}
}
return 0;
}
- The above program prints the numbers from 1 to 10 using a
while loop
in C++ - When
i
becomes 5, thecontinue
statement is encountered. Therefore the iteration ati=5
is skipped - The iteration again starts with
i=6
and prints the value from 1 to 10 skipping 5.
Output
1
2
3
4
6
7
8
9
10
Continue Statement with do...while loop in C++
When acontinue
statement is encountered in the do...while
loop, the control jumps directly to the update expression
, and the remaining code in the loop body gets skipped.Example to demonstrate continue statement with do...while loop in C++
#include <iostream>
using namespace std;
int main()
{
int i = 0;
do {
i++;
if (i == 3) {
continue; //skips the iteration
}
else {
// prints the value of i
cout << i << endl;
}
} while (i < 10);
return 0;
}
- The above program uses the
do...while
loop in C++ to print the numbers from 1 to 10. - When
i=3
, the iteration is skipped and the control goes to theupdate expression
i++
. - The value of
i
increments and the output gets printed from 1 to 10 except 3.
Output
1
2
4
5
6
7
8
9
10
Continue Statement with a nested loop in C++
When thecontinue
statement is used with nested loops in C++, it skips the current iteration of the inner loop
.Example to demonstrate continue statement with a nested loop in C++ Online Editor
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 3; i++) {
for (int j = 0; j <= 5; j++) {
if (j == 3) {
continue;
}
cout << j << " ";
}
cout << endl;
}
return 0;
}
- The above C++ program uses a
nested
loop to print numbers from 1 to 5 in three rows. - The outer loop
i
runs for three times. - In each
ith
iteration, the inner loopj
prints numbers from 1 to 5. - In the inner loop when
j=3
, the iteration is skipped and 3 does not get printed due to thecontinue
statement. - We get three rows of numbers from 1 to 5 except 3 in each row.
Output
1 2 4 5
1 2 4 5
1 2 4 5
Comparison of Break and Continue Statements in C++
Parameters | break statement in C ++ | continue statement in C++ |
Purpose | Exits the current loop | Skips the current iteration |
Applicability | Used within loops (for, while, do-while) | Used within loops (for, while, do-while) |
Effect | Terminates the loop prematurely and continues with the next statement after the loop | Immediately moves to the next iteration of the loop, skipping the remaining code in the current iteration |
Loop Control | Affects the entire loop | Affects only the current iteration of the loop |
Typical Use Cases | When a specific condition is met, and you want to exit the loop | When you want to skip the current iteration of the loop based on a certain condition and continue with the next iteration |
Summary
We saw how thecontinue
statement skips the current iteration and behaves accordingly in all four loops. We even saw the comparison in the behavior of break
and continue
statements in C++. For hands-on and practical understanding, consider our C++ Certification Course.