• Home
  • 5.6 continue in Loops

5.6 continue in Loops

View Categories

5.6 continue in Loops

< 1 min read

Table of Contents

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

The behavior after continue depends on the type of loop. In a for loop, the iteration expression is executed 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: " << number << '\n';
    }

    return 0;
}

Output #

Number: 1
Number: 2
Number: 4
Number: 5

Explanation #

  • The for loop iterates through the values from 1 to 5.
  • The if statement checks whether number is equal to 3.
  • When number is 3, the continue statement is executed.
  • continue skips the remaining statements in the current iteration, so 3 is not printed.
  • For a for loop, execution proceeds to the iteration expression ++number after continue.
  • The loop then evaluates its condition and continues with the next iteration.
  • 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 *