C++ Program to Implement the working of continue statement


January 30, 2023, Learn eTutorial
1147

In this C++ program, we are going to learn about the working of the continue statement.

Define C++ continue statement

In C++ programming, the continue statement is used to skip the loop's current iteration and the program's control goes to the next iteration. The syntax of the continue statement is:

continue;

How does the continue statement work in C++?


for (init; condition; update)
{
   // code
   if (condition to break)
   { 
     continue
     // code
   }
}

In this for loop, continue skips the current iteration and the control flow jumps to the update expression.

 

How to write a C++ program to check the working of Continue?

Here we are using the for loop to print the value of I in each iteration for (int i = 1; i <= 5; i++), here the condition to continue is
if (i == 3)

{
            continue;
}

Which means,

  • When i is equal to 3, the continue statement skips the current iteration and starts the next iteration
  • Then, i becomes 4, and the condition is evaluated again.
  • Hence, 4 and 5 are printed in the next two iterations.

In most cases, the continue statement is used with decision-making statements.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4:  open a for loop with initialization i = 1; condition i <= 5; updation  i++;

Step 5: Set a condition to continue in an if statement i == 3;

Step 6: Print i

Step 7: Exit

C++ Source Code

                                          // program to print the value of i

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        // condition to continue
        if (i == 3) {
            continue;
        }

        cout << i << endl;
    }

    return 0;
}
                                      

OUTPUT

1
2
4
5