Continue statement in C++: Difference between break and continue statement

Continue statement in C++: Difference between break and continue statement

31 Mar 2024
Beginner
738 Views
13 min read
Learn via Video Course & by Doing Hands-on Labs

C++ Programming For Beginners

Continue Statement in C++: An Overview

In this C++ tutorial on jump statements, we will now look at the continue 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++?

The continue 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.

What is the Continue statement in C++

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 a for 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 when i=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 a while 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, the continue statement is encountered. Therefore the iteration at i=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 a continue 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 the update 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 the continue 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 loop j 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 the continue 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++

Parametersbreak statement in C ++continue statement in C++
PurposeExits the current loopSkips the current iteration
ApplicabilityUsed within loops (for, while, do-while)Used within loops (for, while, do-while)
EffectTerminates the loop prematurely and continues with the next statement after the loopImmediately moves to the next iteration of the loop, skipping the remaining code in the current iteration
Loop ControlAffects the entire loopAffects only the current iteration of the loop
Typical Use CasesWhen a specific condition is met, and you want to exit the loopWhen 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 the continue 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.
Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this