Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C++ Continue Statement: Practical Examples & Loop Control

Mastering C++ Continue Statement

This guide explains how the continue statement works in C++ loops, complete with clear examples for for, while, and nested loops.

In C++ programming, the continue keyword instructs a loop to skip the remaining code in the current iteration and immediately evaluate the loop condition or update expression for the next pass.

The syntax is straightforward:

continue;

Before diving into continue, ensure you are comfortable with:


How continue Operates in C++

Mastering C++ Continue Statement: Practical Examples & Loop Control

Example 1: Using continue in a for Loop

In a for loop, continue bypasses the current iteration and jumps directly to the update expression.

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; ++i) {
        if (i == 3) {
            continue; // Skip printing when i equals 3
        }
        cout << i << endl;
    }
    return 0;
}

Output

1
2
4
5

Explanation:

Tip: continue is most effective when paired with conditional logic.


Example 2: Using continue in a while Loop

In a while loop, continue returns control to the loop’s condition check.

#include <iostream>
using namespace std;

int main() {
    int sum = 0, number = 0;

    while (number >= 0) {
        sum += number; // Accumulate positive numbers
        cout << "Enter a number: ";
        cin >> number;

        if (number > 50) {
            cout << "The number is greater than 50 and won't be calculated." << endl;
            number = 0; // Reset to continue prompting
            continue;   // Skip the rest of this loop cycle
        }
    }

    cout << "The sum is " << sum << endl;
    return 0;
}

Sample Run

Enter a number: 12
Enter a number: 0
Enter a number: 2
Enter a number: 30
Enter a number: 50
Enter a number: 56
The number is greater than 50 and won't be calculated.
Enter a number: 5
Enter a number: -3
The sum is 99 

Key points:

Note: The same logic applies to do…while loops.


Using continue in Nested Loops

When placed inside nested loops, continue only affects the innermost loop.

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            if (j == 2) {
                continue; // Skip j == 2 for this i
            }
            cout << "i = " << i << ", j = " << j << endl;
        }
    }
    return 0;
}

Output

i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3

The continue statement skips the inner loop’s iteration where j == 2, then jumps to the next j value.

Remember: break exits the entire loop, whereas continue only skips the current iteration.

C Language

  1. Mastering Conditional Logic in C#: If, If‑Else, If‑ElseIf, and Nested If Statements
  2. Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively
  3. Master C++ Conditional Statements: if, if...else, and Nested if...else Explained
  4. Mastering the C++ break Statement
  5. Mastering the C++ Switch‑Case Statement: Syntax, Workflow, and Practical Examples
  6. Mastering C Control Flow: Break and Continue Statements Explained
  7. Mastering the C Goto Statement: Syntax, Practical Use, and Best Practices
  8. Java Continue Statement – Mastering Loop Control with Examples
  9. Understanding C++ Loop Types: For, While, Do-While Explained
  10. Mastering Decision-Making in C++: If, Switch, and More