Continue Statement in C++

The continue statement in C++ is used inside loops to skip the remaining part of the current iteration and move directly to the next iteration. Unlike break, which ends the loop completely, continue keeps the loop alive and only tells the program to ignore whatever statements are left in the current pass.

This makes continue useful when some iterations should be skipped but the overall loop should still go on. In practical C++ programming, it is often used to ignore invalid values, skip specific numbers, bypass unnecessary work, and keep the main loop logic cleaner by handling exceptions early. To use it correctly, you need to understand exactly where execution jumps after a continue, how it behaves differently in for, while, and do while loops, and how it differs from break.

What Is Continue Statement in C++?

The continue statement is a jump statement used only inside looping constructs. When the program reaches continue, it stops executing the rest of the current iteration and jumps to the next loop cycle according to that loop’s rules.

StatementMain Effect
continueSkips the rest of the current iteration and goes to the next one
breakEnds the loop completely

continue means “skip the rest of this round, but keep looping.”

Syntax of Continue Statement in C++

The syntax is very short. It is simply the keyword followed by a semicolon.

continue;

This statement must appear inside a loop. It is not valid as a general-purpose statement outside looping logic.

How Continue Works in a For Loop

Inside a for loop, continue skips the remaining body statements, then control moves to the update expression, and after that the loop condition is checked again. This detail is important because the update part still happens.

Example of Continue in a For Loop

#include <iostream>

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

        std::cout << i << " ";
    }

    return 0;
}

This prints 1 2 4 5. When i becomes 3, the program skips the std::cout statement for that iteration and moves to the next loop cycle.

How Continue Works in a While Loop

Inside a while loop, continue skips the remaining body statements and jumps back to the loop condition. Because there is no separate update section in the header, the programmer must be careful that the loop variable still changes correctly before or around the continue path.

Example of Continue in a While Loop

#include <iostream>

int main()
{
    int i = 0;

    while (i < 5)
    {
        i++;

        if (i == 3)
        {
            continue;
        }

        std::cout << i << " ";
    }

    return 0;
}

This also prints 1 2 4 5. Notice that i++ happens before the continue. If the update had been placed after it, the loop could behave incorrectly or even become infinite.

How Continue Works in a Do While Loop

Inside a do while loop, continue skips the remaining part of the body and then control moves to the condition check at the bottom of the loop. If that condition is true, the next iteration begins.

#include <iostream>

int main()
{
    int i = 0;

    do
    {
        i++;

        if (i == 2)
        {
            continue;
        }

        std::cout << i << " ";
    } while (i < 5);

    return 0;
}

This prints 1 3 4 5. When i becomes 2, the printing statement is skipped, but the loop itself continues normally.

Why Continue Is Useful in C++

  • It skips unwanted iterations without ending the entire loop.
  • It keeps filtering logic concise when some values should be ignored.
  • It can make code cleaner by handling exceptional cases early.
  • It helps avoid extra nesting when the main loop body should focus only on valid cases.

For example, if a loop processes a list of numbers but should ignore negative values, continue can skip them early and let the rest of the body handle only the useful data. That often reads better than wrapping the whole remaining body inside another if block.

Example of Continue for Filtering Values

#include <iostream>

int main()
{
    int arr[6] = {4, -2, 7, -1, 9, 3};

    for (int i = 0; i < 6; i++)
    {
        if (arr[i] < 0)
        {
            continue;
        }

        std::cout << arr[i] << " ";
    }

    return 0;
}

This prints only the non-negative values. Negative values are skipped, but the loop continues checking the rest of the array.

Continue in Nested Loops in C++

In nested loops, continue affects only the nearest enclosing loop. It does not skip iterations of outer loops automatically. This is the same nearest-loop rule that also applies to break.

#include <iostream>

int main()
{
    for (int row = 1; row <= 3; row++)
    {
        for (int col = 1; col <= 3; col++)
        {
            if (col == 2)
            {
                continue;
            }

            std::cout << row << "," << col << std::endl;
        }
    }

    return 0;
}

Here, continue skips only the iteration of the inner loop when col == 2. The outer loop still continues with the next rows as usual.

Continue vs Break in C++

This comparison is essential because beginners often confuse these two statements. continue keeps the loop alive but skips the rest of the current iteration. break ends the loop entirely.

StatementEffect on current iterationEffect on loop
continueSkips remaining statementsLoop continues with next iteration
breakStops current iteration and all later onesLoop ends completely

If the problem says skip this one case and continue working, use continue. If it says stop the loop entirely because the work is finished or a stop condition was found, use break.

Continue vs Return in C++

continue affects only loop flow. return exits the whole function. That means return is much stronger and leaves the current function entirely, while continue just skips to the next loop cycle.

If the function should end immediately, use return. If the function should keep running but the loop should ignore the rest of the current pass, use continue.

A Common Danger of Continue in While Loops

The biggest practical danger is forgetting to update the loop variable before the continue path in a while or do while loop. Since those loops do not have an automatic update expression like a for loop, the loop state can get stuck and create an infinite loop.

This is why placement of the update statement matters. If the update happens after continue, then the code below it is skipped and the loop variable may never change in that branch.

Continue as an Early Skip Tool

One of the cleanest ways to think about continue is as an early skip tool. A loop may contain several steps, but some iterations may not deserve all of them. Instead of wrapping the rest of the loop body inside a large if block, the program can detect the skip case early, execute continue, and let the normal work happen only for iterations that really matter. This often reduces nesting and keeps the main body focused on the useful path.

Continue in Input Processing Loops

Input-processing loops also benefit from continue when some entered values should simply be ignored. For example, a loop may read numbers continuously but skip blank-style placeholders, invalid markers, or values outside a useful range. In such cases, continue lets the program reject that one iteration quickly and move on to the next input without ending the loop. This creates a natural pattern of read, validate, skip if needed, otherwise process.

Can Continue Be Used in Switch Statements?

By itself, continue is meant for loops, not for using a switch the way break is used there. If a switch appears inside a loop, then a continue inside that context affects the loop, not the switch. This is another reason why break and continue should not be treated as interchangeable just because both change control flow.

Using Continue Without Hurting Readability

continue is most helpful when it removes clutter, not when it scatters control flow everywhere. A loop with one or two clear skip conditions can become easier to read. A loop with too many scattered continue paths can become harder to follow. The goal is to make the valid path simpler, not more confusing.

Common Mistakes with Continue Statement in C++

  • Using continue when break was really needed.
  • Forgetting loop-variable updates before a continue path in while loops.
  • Assuming continue exits the whole loop.
  • Assuming continue affects outer loops in nested-loop structures.
  • Using it so often that the loop becomes harder to read instead of clearer.

The most common beginner mistake is writing a while loop where the update appears after a possible continue. In that situation, the skipped update can keep the condition true forever.

Best Practices for Continue Statement in C++

  • Use continue when it clearly simplifies filtering or skip logic.
  • Be especially careful with updates in while and do while loops.
  • Prefer readability over clever control flow.
  • Remember that only the nearest loop is affected.
  • Choose break instead when the loop should end completely.

Frequently Asked Questions about Continue Statement in C++

Does continue end the loop in C++?

No. It only skips the rest of the current iteration and moves to the next one.

Can continue be used in all loop types?

Yes. It can be used in for, while, and do while loops.

What is the difference between continue and break?

continue skips only the current iteration, while break ends the loop completely.

Why is continue risky in while loops?

Because if the loop variable is not updated before the continue path, the condition may never change and the loop can become infinite.


Continue learning C++ in order
Follow the topic sequence with the previous and next lesson.