• Home
  • 4.7 continue Statement

4.7 continue Statement

View Categories

4.7 continue Statement

< 1 min read

Table of Contents

The continue statement skips the remaining statements in the current iteration of the nearest enclosing loop and proceeds with the next iteration. Unlike break, it does not terminate the loop.

The behavior of continue depends on the type of loop in which it is used. In a for loop, control proceeds to the loop’s iteration expression before the next condition check.

Source Code #

#include <iostream>

int main()
{
    for (int number = 1; number <= 5; ++number)
    {
        if (number == 3)
        {
            continue;
        }

        std::cout << number << '\n';
    }

    return 0;
}

Output #

1
2
4
5

Explanation #

  • The continue statement skips the remaining statements in the current loop iteration.
  • The for loop generates values from 1 through 5.
  • When number becomes 3, the condition number == 3 evaluates to true.
  • continue is executed, so the std::cout statement is skipped for that iteration.
  • Execution then proceeds with the loop’s iteration expression, ++number.
  • The loop continues normally with the next value, 4.
  • Unlike break, continue does not terminate the loop.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *