Conditions and Loops

break and continue

The keywords break and continue can be used within loops to change their normal flow of execution. Each is typed as a single word with a semicolon immediately after it.

The keyword break causes the loop to terminate immediately, and execution of the program continues with the first statement following the loop.

If further iterations remain when the loop terminates, those iterations are ignored.

A loop might test for a character being encountered on an input line and break from the loop as soon as it is found.

The break keyword can be used in while, for, and do while loops.

#include <iostream>

int main()
{
    // Use break to exit the loop once the number 7 is found.
    for (int i{1}; i <= 10; ++i)
    {
        if (i == 7)
        {
            break;
        }

        std::cout << i << ' ';
    }

    std::cout << "\nLoop ended.\n";
}

Output:

1 2 3 4 5 6
Loop ended.

The loop begins with i equal to 1 and prints each value as it increases. When i becomes 7, the break statement executes before the output statement. The entire loop ends, so 7 and the values that follow it are not printed.

The continue statement abandons the remaining statements of the current iteration and begins the next pass through the loop.

In a for loop, execution passes to the update expression in the third part of the loop header. The condition is then tested again to determine whether another iteration should begin.

In while and do while loops, execution proceeds to the controlling condition to determine whether the loop should continue.

When a sequence of values is being processed, continue can be used to ignore the current value without terminating the entire operation.

#include <iostream>

int main()
{
    // Use continue to skip the number 7.
    for (int i{1}; i <= 10; ++i)
    {
        if (i == 7)
        {
            continue;
        }

        std::cout << i << ' ';
    }

    std::cout << "\nLoop ended.\n";
}

Output:

1 2 3 4 5 6 8 9 10
Loop ended.

When i becomes 7, continue skips the output statement for that iteration. The update expression still increments i, and the loop continues with 8.

Use continue with caution in a while or do while loop when the controlling variable is updated inside the body. If continue is reached before that update occurs, the variable may never change and the loop may run forever.

#include <iostream>

int main()
{
    int value{0};

    while (value < 5)
    {
        // Increment the value before the expression uses it
        ++value;

        if (value == 3)
        {
            continue;
        }

        std::cout << value << ' ';
    }
}

In this example, value is incremented before continue can execute. This ensures that the loop still makes progress when the value 3 is skipped.

break is often used to terminate a loop when a specific condition inside it has been met.

// Include the upper boundary in this loop
for (int value{1}; value <= 100; ++value)
{
    if (value == 25)
    {
        std::cout << "Value found\n";
        break;
    }
}

continue is often used when one particular iteration is not useful, but the remaining iterations should still run.

for (int value{1}; value <= 10; ++value)
{
    // Use the remainder operator to keep what division leaves over
    if (value % 2 == 0)
    {
        continue;
    }

    std::cout << value << ' ';
}

This loop skips every even number and prints only the odd numbers.

A large number of break and continue statements within one loop body may indicate that the loop's logic should be reorganized or separated into functions to make it easier to follow.

#include <iostream>

int main()
{
    for (int value{1}; value <= 10; ++value)
    {
        if (value == 7)
        {
            break;
        }

        std::cout << value << ' ';
    }

    std::cout << "\nThe first loop ended.\n";

    for (int value{1}; value <= 10; ++value)
    {
        if (value == 7)
        {
            continue;
        }

        std::cout << value << ' ';
    }

    std::cout << "\nThe second loop ended.\n";
}

The first loop stops completely when its value reaches 7. The second loop skips only the iteration where the value is 7 and then continues processing the remaining values.

The difference is that break leaves the loop, while continue leaves only the current iteration.

Exercise: break and continueStarter code, hints and solutionOpenClose
Guided practiceBeginner · 10–15 min

Practice: break and continue

Work through the mission, reveal hints only when needed, and compare your result with the model solution.

Your challenge

Let the first loop print one more value before breaking. Change its actual stopping condition, not just its for-loop limit; leave the continue example unchanged.

Mission checklist

  1. Run the starter as a baseline, then predict the change requested in Your challenge.
  2. Make that change in your editor; use the lesson-specific hints if you need help.
  3. Compile and run your answer, then compare with the worked solution and explain the result.

Starter program

This is the working baseline, not the completed answer. Compile as C++20, then make the change described above.

#include <iostream>

int main()
{
    for (int value{1}; value <= 10; ++value)
    {
        if (value == 7)
        {
            break;
        }

        std::cout << value << ' ';
    }

    std::cout << "\nThe first loop ended.\n";

    for (int value{1}; value <= 10; ++value)
    {
        if (value == 7)
        {
            continue;
        }

        std::cout << value << ' ';
    }

    std::cout << "\nThe second loop ended.\n";
}

Hints

Hint 1

In the first loop, value == 7 breaks out before printing 7. Raising the loop limit alone will not get past that break.

Hint 2

In the second loop, continue skips only the output for 7; the for-loop increment still happens. Compare the values printed after 7 in the two loops.

Worked solution

The first loop stops at its break, not at the for-loop limit. Moving the break boundary from value == 7 to value == 8 lets it process one more value, printing 7 as well. The second loop and its continue behavior are unchanged.

#include <iostream>

int main()
{
    for (int value{1}; value <= 10; ++value)
    {
        if (value == 8)
        {
            break;
        }

        std::cout << value << ' ';
    }

    std::cout << "\nThe first loop ended.\n";

    for (int value{1}; value <= 10; ++value)
    {
        if (value == 7)
        {
            continue;
        }

        std::cout << value << ' ';
    }

    std::cout << "\nThe second loop ended.\n";
}

Example output

1 2 3 4 5 6 7 
The first loop ended.
1 2 3 4 5 6 8 9 10 
The second loop ended.

0 comments

No comments yet. Be the first.

Add a comment

About CppLearn

CppLearn is an interactive, free reference for modern C++. In each lesson, you'll find out how the language works, rather than just lines of code that you can copy and paste. We cover concepts starting from beginner lessons on fundamentals and proceed to templates, move semantics, smart pointers, and areas that usually trap or scare C++ learners.