c-plus-plus
  1. c-plus-plus-continue-statement

C++ Control Statement - Continue Statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration.

Syntax

The syntax for continue statement is:

continue;

Example

#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i <= 10; i++) {
        if(i % 2 == 0) {
            continue;
        }
        cout << i << " ";
    }

    return 0;
}

Output

1 3 5 7 9

Explanation

In the above example, the for loop iterates through the numbers 1 to 10. For each iteration, the if statement checks if the current number is even by checking if the remainder of the number divided by 2 is equal to 0. If the number is even, the continue statement is executed, which skips the current iteration and moves on to the next iteration. If the number is odd, the cout << i << " "; statement is executed, which prints the current number to the console.

Use

The continue statement is used when you want to skip the current iteration of a loop for some reason, but still continue with the loop.

Important Points

  • The continue statement can only be used inside loops.
  • The continue statement causes the control to immediately jump to the next iteration of the loop.
  • The continue statement skips all remaining statements in the current iteration of the loop.

Summary

The continue statement is used to skip the current iteration of a loop and move to the next iteration. It can only be used inside loops and causes the control to immediately jump to the next iteration of the loop. The continue statement is useful when you want to skip some iterations of a loop for some reason, but still continue with the loop.

Published on: