In this C++ program, we are going to learn about the working of the 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;
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.
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,
In most cases, the continue statement is used with decision-making statements.
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
// 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;
}
1 2 4 5