Topic 20 of 91 3. Control Flow in C++ Interactive Tutorial

The continue Statement in C++: Skipping Selected Iterations

10 mins read Last updated: 26 Sep 2026 16,100 views

Direct Answer & Core Concept

The continue statement in C++ is used to skip the remaining statements in the current loop iteration and move to the next iteration. It is useful when you want a loop to ignore specific values or conditions without stopping the entire loop.

For example, suppose you want to print numbers from 1 to 10 but skip the number 5. Instead of ending the loop, you can use the continue statement to skip that particular iteration.

Detailed Explanation & Mechanics

1. What Is the continue Statement in C++?

The continue statement is a jump statement that skips the remaining statements in the current iteration of a loop.

When C++ encounters continue:

  1. The remaining statements in the current iteration are skipped.

  2. The loop moves towards its next iteration.

  3. The loop's condition is checked again when required.

Syntax

The continue statement can be used inside:

  • for loops

  • while loops

  • do-while loops

  • Range-based for loops

It cannot be used in a switch statement by itself because switch is not a loop.

2. Simple Example of continue in C++

Let's start with a simple example that prints numbers from 1 to 5 but skips the number 3.

main.cpp
Run Online
#include <iostream>
using namespace std;

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

        cout << i << " ";
    }

    return 0;
}

Output:

Explanation:

  • The loop starts with i = 1.

  • The program prints 1 and 2.

  • When i becomes 3, the condition i == 3 becomes true.

  • The continue statement skips the cout statement for that iteration.

  • The loop continues with 4 and 5.

The important point is that the loop does not terminate when continue executes.

3. Using continue in a for Loop

A for loop is commonly used when you know how many times a task needs to be repeated.

The continue statement skips the remaining statements in the current iteration. In a traditional for loop, execution then moves to the update expression before checking the loop condition again.

Example: Skip Even Numbers

main.cpp
Run Online
#include <iostream>
using namespace std;

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

        cout << i << " ";
    }

    return 0;
}

Output:

Explanation:

  • The loop runs from 1 to 10.

  • The condition i % 2 == 0 checks whether a number is even.

  • If the number is even, continue skips the print statement.

  • Only odd numbers are displayed.

Example: Skip a Specific Number

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 7; i++) {
        if (i == 4) {
            continue;
        }

        cout << "Number: " << i << endl;
    }

    return 0;
}

Output:

main.cpp
Run Online
Number: 1
Number: 2
Number: 3
Number: 5
Number: 6
Number: 7

Here, the loop skips 4 but continues processing all the other numbers.

4. Using continue in a while Loop

A while loop executes as long as its condition remains true.

When continue is executed inside a while loop, the remaining statements in the current iteration are skipped, and the loop condition is checked again.

Example: Skip Number 5

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {
    int i = 1;

    while (i <= 7) {
        if (i == 5) {
            i++;
            continue;
        }

        cout << i << " ";
        i++;
    }

    return 0;
}

Output:

Explanation:

  • The variable i starts at 1.

  • The loop prints numbers until it reaches 5.

  • When i is 5, the program increments i and executes continue.

  • The print statement is skipped for that iteration.

  • The loop continues with 6 and 7.

Important: In a while loop, make sure your loop variable is updated before executing continue when the update would otherwise be skipped. Forgetting this can create an infinite loop.

5. Using continue in a do-while Loop

A do-while loop executes its body at least once before checking the loop condition.

When continue executes inside a do-while loop, control moves to the condition check at the end of the loop.

Example: Skip Even Numbers

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {
    int i = 0;

    do {
        i++;

        if (i % 2 == 0) {
            continue;
        }

        cout << i << " ";

    } while (i < 10);

    return 0;
}

Output:

Explanation:

  • The variable i starts at 0.

  • It is incremented at the beginning of each iteration.

  • If the number is even, continue skips the print statement.

  • The loop condition is checked.

  • The loop continues until i reaches 10.

6. Using continue in a Range-Based for Loop

A range-based for loop is used to process each element in an array, container, or other suitable range.

You can use continue to skip selected elements.

Example: Skip a Specific Array Element

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {
    int numbers[] = {10, 20, 30, 40, 50};

    for (int number : numbers) {
        if (number == 30) {
            continue;
        }

        cout << number << " ";
    }

    return 0;
}

Output:

Explanation:

  • The range-based loop processes each array element.

  • When the value is 30, continue skips the print statement.

  • The next element is processed.

  • All other values are printed.

7. Practical Example: Print Only Positive Numbers

Suppose you have an array containing positive, negative, and zero values. You want to display only positive numbers.

You can use continue to skip values that are zero or negative.

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {
    int numbers[] = {10, -5, 20, 0, -8, 30};

    for (int number : numbers) {
        if (number <= 0) {
            continue;
        }

        cout << number << " ";
    }

    return 0;
}

Output:

Explanation:

The condition number <= 0 identifies zero and negative values. The continue statement skips those elements, so only positive numbers are displayed.

8. Using continue in Nested Loops

A nested loop means one loop is placed inside another loop.

When continue is used inside an inner loop, it skips the current iteration of that inner loop. The outer loop continues normally.

Example: Skip One Value in an Inner Loop

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 4; j++) {

            if (j == 3) {
                continue;
            }

            cout << i << " " << j << endl;
        }
    }

    return 0;
}

Output:

main.cpp
Run Online
1 1
1 2
1 4
2 1
2 2
2 4
3 1
3 2
3 4

Explanation:

  • The outer loop runs three times.

  • The inner loop runs from 1 to 4 during each outer iteration.

  • When j becomes 3, continue skips the current inner-loop iteration.

  • The inner loop proceeds to 4.

  • The outer loop continues normally.

9. Using continue Inside a switch Nested in a Loop

A continue statement cannot be used in a standalone switch statement. However, it can be used inside a switch that is itself inside a loop.

In that situation, continue applies to the nearest enclosing loop, not the switch.

Example: Skip a Value Using switch

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        switch (i) {
            case 3:
                continue;

            default:
                cout << i << " ";
        }
    }

    return 0;
}

Output:

Explanation:

  • The for loop processes numbers from 1 to 5.

  • The switch checks the current value.

  • When i is 3, continue skips the rest of the current loop iteration.

  • The loop moves to the next value.

10. Difference Between break and continue in C++

Both break and continue are jump statements, but they control loops differently.

Featurebreakcontinue
PurposeTerminates the nearest enclosing loop or switchSkips the remaining statements in the current loop iteration
Loop executionStops the loopContinues with the next iteration
Used in for loopYesYes
Used in while loopYesYes
Used in do-while loopYesYes
Used in switch aloneYesNo
Typical useStop searching after finding a valueSkip unwanted values

Example Using break

main.cpp
Run Online
#include <iostream>
using namespace std;

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

        cout << i << " ";
    }

    return 0;
}

Output:

The loop stops completely when i becomes 3.

Example Using continue

main.cpp
Run Online
#include <iostream>
using namespace std;

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

        cout << i << " ";
    }

    return 0;
}

Output:

The loop skips 3 but continues processing the remaining values.

11. Common Mistakes When Using continue

Mistake 1: Forgetting to Update a while Loop Variable

Incorrect example:

main.cpp
Run Online
int i = 1;

while (i <= 5) {
    if (i == 3) {
        continue;
    }

    cout << i << " ";
    i++;
}

Problem: When i becomes 3, the continue statement runs before i++. The value remains 3, so the condition stays true and the loop never ends.

Correct example:

main.cpp
Run Online
int i = 1;

while (i <= 5) {
    if (i == 3) {
        i++;
        continue;
    }

    cout << i << " ";
    i++;
}

Output:

Mistake 2: Expecting continue to Exit the Loop

The continue statement does not terminate the loop. It only skips the current iteration.

Use break if you want to stop the loop completely.

Mistake 3: Using continue Outside a Loop

Incorrect example:

main.cpp
Run Online
int main() {
    int number = 10;

    if (number == 10) {
        continue;
    }

    return 0;
}

This code produces a compilation error because continue is not inside a loop.

Mistake 4: Expecting continue to Skip an Entire switch Statement

The continue statement does not directly terminate a switch block. When a switch is inside a loop, continue skips the current iteration of that enclosing loop.

12. Best Practices for Using continue in C++

Follow these practices when using continue:

  1. Use continue when you want to skip unwanted values without stopping the loop.

  2. Keep the condition before the statements you want to skip.

  3. In while loops, ensure the loop variable or other progress condition is updated before a possible continue.

  4. In nested loops, remember that continue applies to the nearest enclosing loop.

  5. Avoid using too many continue statements if they make the program difficult to understand.

  6. Use meaningful conditions so other developers can easily understand why an iteration is skipped.

  7. Test boundary values and conditions that might cause an infinite loop.

13. Frequently Asked Questions (FAQs)

Q1. What is the continue statement in C++?

The continue statement skips the remaining statements in the current iteration of a loop and moves execution towards the next iteration.

Q2. Can we use continue in a for loop?

Yes. You can use continue inside a for loop to skip selected iterations. The loop's update expression still executes before the next condition check.

Q3. Can we use continue in a while loop?

Yes. In a while loop, continue skips the remaining statements and moves control to the next condition check.

Q4. Can continue be used in a do-while loop?

Yes. In a do-while loop, continue skips the remaining statements and moves control to the condition check at the end of the loop.

Q5. Can we use continue in a switch statement?

No, not in a standalone switch statement. However, if the switch is inside a loop, continue can be used to continue the enclosing loop.

Q6. What is the difference between break and continue?

break terminates the nearest enclosing loop or switch, whereas continue skips the current loop iteration and proceeds with the next one.

Q7. Does continue work in nested loops?

Yes. It skips the current iteration of the nearest enclosing loop. Other enclosing loops continue normally.

Q8. Does continue increase the loop variable automatically?

Not always. In a traditional for loop, the update expression runs after continue. In while and do-while loops, you must ensure your code updates the loop variable where necessary.

Runnable Code Example: The continue Statement in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>

int main() {
    std::cout << "Printing odd numbers between 1 and 10:
";
    for (int i = 1; i <= 10; ++i) {
        // Skip even numbers
        if (i % 2 == 0) {
            continue; // Skip the rest of the loop body
        }
        std::cout << i << " ";
    }
    std::cout << "
";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is the key behavioral difference between continue in a for loop vs a while loop?

A: In a `for` loop, `continue` jumps to the increment expression (`++i`) before checking the condition. In a `while` loop, it jumps directly to the condition; if the counter is updated at the bottom of the while body, an infinite loop can occur if not handled carefully.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Use continue as an early guard check at the top of loops to avoid deeply nested if statements.
2. Ensure loop update variables in while loops are incremented before invoking continue to avoid infinite loops.