C++ Program to Implement the working of continue statement

Here, we will learn about the continue statement and its works with for loop.

C++ continue statement

In computer programming, the continue statement is used to skip the current iteration of the loop and the control of the program goes to the next iteration.
The syntax of the continue statement is:
continue;

Working of C++ continue statement with for loop

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

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

C++ program to implement the working of continue statement in C++

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;
}

This 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.
The continue statement is almost always 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