Lesson 32 of 60 – continue Statement in C++
53%

continue Statement in C++

The continue statement is used to skip the remaining statements of the current iteration of a loop and move to the next iteration. Unlike break, the continue statement does not terminate the loop.

Note: Use continue when you want to skip a particular iteration but continue executing the loop.

1. What is the continue Statement?

The continue statement skips the remaining part of the current loop iteration.

continue;

After continue executes, the loop proceeds according to its normal iteration rules.

2. Syntax of continue

continue;

The continue statement consists of the keyword continue followed by a semicolon.

3. Simple continue Example

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    std::cout << i << std::endl;
}

Output:

1
2
4
5

When i is 3, the current iteration is skipped.

4. How continue Works

Suppose we have this loop:

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    std::cout << i;
}

The execution is:

  1. Print 1.
  2. Print 2.
  3. When i == 3, continue executes.
  4. The remaining statements for that iteration are skipped.
  5. The loop moves to the next iteration.
  6. Print 4.
  7. Print 5.

5. continue with for Loop

for (int i = 1; i <= 10; i++) {

    if (i == 5) {
        continue;
    }

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

Output:

1 2 3 4 6 7 8 9 10

Only 5 is skipped.

6. continue with while Loop

With a while loop, make sure the loop variable is updated before continue, otherwise the loop can become infinite.

int i = 0;

while (i < 5) {

    i++;

    if (i == 3) {
        continue;
    }

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

Output:

1 2 4 5

7. continue with do-while Loop

int i = 0;

do {

    i++;

    if (i == 3) {
        continue;
    }

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

} while (i < 5);

Output:

1 2 4 5

The value 3 is skipped, but the loop continues.

8. Skip Even Numbers

for (int i = 1; i <= 10; i++) {

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

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

Output:

1 3 5 7 9

The continue statement skips all even numbers.

9. Print Only Even Numbers

for (int i = 1; i <= 10; i++) {

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

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

Output:

2 4 6 8 10

Here, odd numbers are skipped.

10. Skip Multiples of 5

for (int i = 1; i <= 20; i++) {

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

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

The numbers 5, 10, 15 and 20 are skipped.

11. continue with if Statement

The continue statement is often used inside an if statement.

for (int i = 1; i <= 10; i++) {

    if (i < 5) {
        continue;
    }

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

Only numbers from 5 to 10 are displayed.

12. continue with User Input

int number;

for (int i = 1; i <= 5; i++) {

    std::cout << "Enter number: ";
    std::cin >> number;

    if (number < 0) {
        continue;
    }

    std::cout << "Number = "
              << number
              << std::endl;
}

Negative numbers are skipped.

13. continue with Array

int numbers[] = {10, -5, 20, -8, 30};

for (int i = 0; i < 5; i++) {

    if (numbers[i] < 0) {
        continue;
    }

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

Output:

10 20 30

Negative values are ignored.

14. continue with String

#include <iostream>
#include <string>

int main() {

    std::string text = "HELLO";

    for (int i = 0; i < text.length(); i++) {

        if (text[i] == 'L') {
            continue;
        }

        std::cout << text[i];
    }

    return 0;
}

Output:

HEO

Both occurrences of L are skipped.

15. Skip a Specific Number

int skipNumber = 7;

for (int i = 1; i <= 10; i++) {

    if (i == skipNumber) {
        continue;
    }

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

The number stored in skipNumber is skipped.

16. continue in Multiplication Table

int number = 5;

for (int i = 1; i <= 10; i++) {

    if (i == 5) {
        continue;
    }

    std::cout << number
              << " x "
              << i
              << " = "
              << number * i
              << std::endl;
}

The fifth multiplication is skipped.

17. continue in Nested Loops

When continue is used inside nested loops, it affects the nearest enclosing loop.

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

    for (int j = 1; j <= 5; j++) {

        if (j == 3) {
            continue;
        }

        std::cout << j << " ";
    }

    std::cout << std::endl;
}

The inner loop skips 3 during every outer iteration.

18. continue Does Not Terminate the Loop

for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

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

std::cout << "Loop finished";

The loop continues after skipping 3 and eventually finishes normally.

19. continue vs break

continue break
Skips the current iteration. Terminates the loop.
Loop continues. Loop stops.
Used when an iteration should be ignored. Used when no more iterations are required.
// continue
if (i == 5) {
    continue;
}

// break
if (i == 5) {
    break;
}

20. continue vs if-else

Sometimes the same result can be achieved using if-else. However, continue can make loop logic easier to read when unwanted cases need to be skipped.

for (int i = 1; i <= 10; i++) {

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

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

The code focuses directly on the values that should be processed.

21. continue with Validation

int marks[] = {75, -1, 82, 90, -5};

for (int i = 0; i < 5; i++) {

    if (marks[i] < 0) {
        continue;
    }

    std::cout << "Marks: "
              << marks[i]
              << std::endl;
}

Invalid negative values are skipped while valid marks are processed.

22. continue with Positive Numbers

int numbers[] = {-10, 20, -30, 40, 50};

for (int i = 0; i < 5; i++) {

    if (numbers[i] <= 0) {
        continue;
    }

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

Output:

20 40 50

Only positive numbers are processed.

23. continue with User Choice

char choice;

for (int i = 1; i <= 5; i++) {

    std::cout << "Enter y or n: ";
    std::cin >> choice;

    if (choice == 'n') {
        continue;
    }

    std::cout << "Processing..."
              << std::endl;
}

When the user enters n, processing for that iteration is skipped.

24. continue in do-while and Condition Check

int i = 0;

do {

    i++;

    if (i == 3) {
        continue;
    }

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

} while (i < 5);

For a do-while loop, after continue is executed, control moves toward the loop's condition check.

Output:

1 2 4 5

25. Avoid Infinite Loops with continue

Be careful when using continue inside while and do-while loops. The loop variable must still be updated.

Incorrect example:

int i = 1;

while (i <= 5) {

    if (i == 3) {
        continue;
    }

    i++;
}

When i becomes 3, continue executes before i++, so the loop can remain stuck at 3.

Better approach:

int i = 1;

while (i <= 5) {

    i++;

    if (i == 3) {
        continue;
    }

    std::cout << i;
}

26. Common Mistakes with continue

  • Thinking continue terminates the loop.
  • Forgetting to update the loop variable in a while loop.
  • Using continue when break is actually required.
  • Putting continue in the wrong condition.
  • Not understanding where execution resumes.
  • Using too many continue statements and making code difficult to read.
  • Forgetting that continue affects the nearest enclosing loop.

27. Practical Example – Skip Invalid Data

#include <iostream>

int main() {

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

    for (int i = 0; i < 6; i++) {

        if (numbers[i] <= 0) {
            continue;
        }

        std::cout << "Valid number: "
                  << numbers[i]
                  << std::endl;
    }

    return 0;
}

The program ignores zero and negative values and processes only positive numbers.

28. Practical Example – Student Marks

#include <iostream>

int main() {

    int marks[] = {85, -1, 72, 91, -5, 68};

    for (int i = 0; i < 6; i++) {

        if (marks[i] < 0) {
            continue;
        }

        std::cout << "Student Marks: "
                  << marks[i]
                  << std::endl;
    }

    return 0;
}

Invalid marks are skipped, while valid marks are displayed.

29. Best Practices for continue

  • Use continue when the current iteration should be skipped.
  • Use break when the entire loop should stop.
  • Keep the continue condition simple.
  • Make sure loop variables are updated correctly.
  • Be especially careful with continue in while and do-while loops.
  • Use continue to ignore invalid or unwanted data.
  • Avoid excessive use of continue if it makes the code harder to understand.

30. continue Statement – Final Summary

Concept Meaning
continue; Skips the remaining statements of the current iteration.
for Loop Moves to the next iteration of the for loop.
while Loop Moves toward the next condition check.
do-while Loop Moves toward the condition check at the end of the loop.
Nested Loop continue affects the nearest enclosing loop.
break Terminates the loop completely.
if (condition) {
    continue;
}

📌 Key Points

  • The continue statement skips the current iteration.
  • It does not terminate the loop.
  • It can be used with for, while, and do-while loops.
  • It is commonly used inside an if statement.
  • It is useful for skipping unwanted or invalid data.
  • In a for loop, control moves to the update expression.
  • In a while loop, control moves toward the condition check.
  • In a do-while loop, control moves toward the condition check at the bottom.
  • In nested loops, continue affects the nearest enclosing loop.
  • Always make sure the loop variable is updated properly to avoid infinite loops.

🧠 Quick Quiz

Question: What does the continue statement do in a loop?