C++ continue statement



In the break statement tutorial we have seen the way by using which we can terminate the loop whenever require. Similarly we can use continue statement to skip the part of the loop.

C++ continue statement :

The continue statement forces the next iteration of the loop to take place, skipping remaining code in between.

Continue-Statement-in-CPP

  1. In the case of the for loop as soon as after the execution of continue statement, increment/decrement statement of the loop gets executed. After the execution of increment statement, condition will be checked.
  2. In case of the while loop, continue statement will take control to the condition statement.
  3. In case of the do..while loop, continue statement will take control to the condition statement specified in the while loop.

Example #1. Continue statement :

#include <iostream>
using namespace std;
 
int main ()
{
   int count = 0;
   do
   {	   
     count++;
     if(count > 5 && count < 7)
        continue;

     cout << "Count : " << count << endl;
   }while( count < 10 );

   return 0;
}

Output :

Count : 1
Count : 2
Count : 3
Count : 4
Count : 5
Count : 7
Count : 8
Count : 9
Count : 10

In the above example, when count = 6 then both conditions become true and thus continue statement gets executed in that case.

C++ Continue Statement Flowchart :

cpp_continue_statement
Courtesy : Tutorialspoint