C++ continue statement


Introduction

The continue statement in C++ is used to skip the current iteration of a loop and continue with the next iteration. Unlike the break statement, which terminates the loop entirely, the continue statement only skips the remaining code in the current iteration and proceeds to the next iteration of the loop.

In this blog post, we'll cover:

  • The purpose and syntax of the continue statement.
  • How to use it in different types of loops (for, while, do...while).
  • Practical examples demonstrating how and when to use the continue statement.

1. Syntax of the continue Statement

The syntax of the continue statement is straightforward:

continue;
  • When executed, it skips the rest of the code inside the loop's body for the current iteration.
  • The loop then proceeds with the next iteration (if the loop condition is still valid).

2. Using the continue Statement in for Loops

The continue statement in a for loop skips the remaining statements inside the loop and jumps to the next iteration. This is useful when you want to skip over certain iterations based on a condition but keep the loop running.

Example 1: Skipping Even Numbers in a for Loop

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue;  // Skip even numbers
        }
        cout << "Odd number: " << i << endl;
    }
    
    return 0;
}

Output:

Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9

Explanation:

  • The continue statement is executed when the value of i is even (i % 2 == 0).
  • The loop skips the cout statement for even numbers and only prints odd numbers.

3. Using the continue Statement in while Loops

In a while loop, the continue statement works similarly by skipping the rest of the code inside the loop's body and jumping back to the condition check for the next iteration.

Example 2: Skipping Negative Numbers in a while Loop

#include <iostream>
using namespace std;

int main() {
    int i = -5;
    
    while (i <= 5) {
        i++;  // Increment i
        if (i < 0) {
            continue;  // Skip negative numbers
        }
        cout << "Positive number: " << i << endl;
    }
    
    return 0;
}

Output:

Positive number: 0
Positive number: 1
Positive number: 2
Positive number: 3
Positive number: 4
Positive number: 5

Explanation:

  • The continue statement is executed when i is less than 0, causing the loop to skip negative numbers and only print positive numbers.
  • The i++ ensures that i is incremented with each iteration, but negative numbers are skipped when they occur.

4. Using the continue Statement in do...while Loops

In a do...while loop, the continue statement is used to skip the current iteration and return to the loop's condition check. Since the do...while loop always executes the block of code at least once, the continue statement ensures the loop moves to the next iteration without executing the remaining statements.

Example 3: Skipping Even Numbers in a do...while Loop

#include <iostream>
using namespace std;

int main() {
    int i = 1;
    
    do {
        if (i % 2 == 0) {
            i++;
            continue;  // Skip even numbers
        }
        cout << "Odd number: " << i << endl;
        i++;
    } while (i <= 10);
    
    return 0;
}

Output:

Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9

Explanation:

  • The loop runs at least once because of the do...while structure.
  • The continue statement skips the even numbers, printing only the odd ones.

5. Common Use Cases for the continue Statement

  • Skipping specific iterations: When certain conditions require you to ignore or skip processing for specific iterations without terminating the loop.
  • Avoiding unnecessary computations: If certain calculations or actions in an iteration are not needed, you can skip them using continue to optimize the flow.
  • Skipping invalid input or values: In scenarios like input validation, you might want to skip over invalid inputs and continue processing the rest of the valid data.

6. Example 4: Skipping Invalid User Input

You can use the continue statement to skip invalid user input and continue asking the user for valid input.

#include <iostream>
using namespace std;

int main() {
    int number;
    
    // Ask the user for numbers until a valid one is entered
    while (true) {
        cout << "Enter a positive number: ";
        cin >> number;
        
        if (number <= 0) {
            cout << "Invalid input! Please enter a positive number." << endl;
            continue;  // Skip invalid input
        }
        
        cout << "You entered: " << number << endl;
        break;  // Exit the loop when a valid number is entered
    }
    
    return 0;
}

Output:

Enter a positive number: -3
Invalid input! Please enter a positive number.
Enter a positive number: 5
You entered: 5

Explanation:

  • If the user enters a non-positive number, the continue statement is executed, skipping the cout for invalid input and prompting the user again until a valid number is entered.

7. Key Differences Between continue and break Statements

Feature continue break
Purpose Skips the current iteration and moves to the next one. Exits the loop or switch statement entirely.
Loop Termination Does not terminate the loop; only skips the iteration. Terminates the loop or switch immediately.
Use Case Skipping unwanted or invalid iterations. Breaking out of a loop or switch prematurely.