Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator

What is the continue statement in C++?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The continue statement in C++ is used to stop an iteration that is running and continue with the next one.

How does it work?

The continue statement works in such a way that it cancels every statement running in the iteration of a loop and moves the control back to the top of the loop. In other words, it continues to the next iteration when a certain condition is reached.

In C++, we can use the continue statement in the while and for loops.

Using the continue statement in a for loop

In the example below, we will use the continue statement in a for loop to continue to the next iteration when the value of x equals 5.

Code example

#include <iostream>
using namespace std;
int main() {
for (int x = 0; x < 10; x++) {
if (x == 5) {
continue;
}
cout << x << "\n";
}
return 0;
}
Using the continue statement in a for loop

Code explanation

  • Line 3: We use the continue statement to continue to the next iteration after the number '5' character in the range of values we created.

  • Line 5: We start a for loop and give an expression that makes x be in the range of 0 to 9.

  • Line 6: We create a condition that states that if the value of x is equal to 5 in the iteration, the iteration should stop at that point and continue to the next iteration 6, using the continue statement.

  • Line 9: We print the statement or command to execute provided the condition provided is true.

Using the continue statement in a while loop

In the code below, we will use the continue statement in a while loop to continue to the next iteration when x equals 5.

Code example

#include <iostream>
using namespace std;
int main() {
int x = 0;
while (x < 10) {
if (x == 5) {
x++;
continue;
}
cout << x << "\n";
x++;
}
return 0;
}
Using the continue statement in a while loop

Code explanation

  • Line 5: We create an integer variable x and assign it to 0.
  • Line 6: Using the while loop, we make the values of x to be less than 10.
  • Lines 7, 8, and 9: Using the if statement in line 7, we create a condition that if x equals 5, and the value of x incrementing by 1 in line 8, the iteration should stop at that point and continue to the next value of x, which is 6 using the continue statement in line 9.
  • Line 11: We print all the values of x providing the condition provided in line 7 is true.
  • Line 12: We increment x by 1.

RELATED TAGS

c++
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring