C++ continue Statement

Introduction

The continue statement in C++ is used to skip the rest of the code inside a loop for the current iteration and immediately proceed to the next iteration of the loop. It is particularly useful when you want to skip certain conditions within a loop without exiting the loop entirely, which is what the break statement does.

Using continue in Loops

Example: Using continue in a for Loop

In this example, we use the continue statement to skip the even numbers in a for loop.

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            continue; // Skip the rest of the loop body for even numbers
        }
        cout << "i = " << i << endl;
    }

    return 0;
}

Output

i = 1
i = 3
i = 5
i = 7
i = 9

Explanation

  • The for loop initializes i to 0 and increments it by 1 in each iteration.
  • The condition i % 2 == 0 checks if i is an even number.
  • If i is even, the continue statement is executed, skipping the rest of the loop body for that iteration.
  • If i is odd, the value of i is printed.

Example: Using continue in a while Loop

In this example, we use the continue statement to skip the number 5 in a while loop.

#include <iostream>
using namespace std;

int main() {
    int i = 0; // Initialization of loop control variable

    while (i < 10) {
        i++; // Increment the loop control variable
        if (i == 5) {
            continue; // Skip the rest of the loop body when i equals 5
        }
        cout << "i = " << i << endl;
    }

    return 0;
}

Output

i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
i = 10

Explanation

  • The while loop continues to execute as long as i is less than 10.
  • The loop control variable i is incremented by 1 in each iteration.
  • When i equals 5, the continue statement is executed, skipping the rest of the loop body for that iteration.
  • The value of i is printed for all other values.

Example: Using continue in a do-while Loop

In this example, we use the continue statement to skip the number 5 in a do-while loop.

#include <iostream>
using namespace std;

int main() {
    int i = 0; // Initialization of loop control variable

    do {
        i++; // Increment the loop control variable
        if (i == 5) {
            continue; // Skip the rest of the loop body when i equals 5
        }
        cout << "i = " << i << endl;
    } while (i < 10); // Condition

    return 0;
}

Output

i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
i = 10

Explanation

  • The do-while loop ensures that the loop body is executed at least once.
  • The loop control variable i is incremented by 1 in each iteration.
  • When i equals 5, the continue statement is executed, skipping the rest of the loop body for that iteration.
  • The value of i is printed for all other values.

Example: Skipping Negative Numbers in an Array

In this example, we use the continue statement to skip negative numbers while printing the elements of an array.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, -2, 3, -4, 5};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array

    for (int i = 0; i < size; i++) {
        if (arr[i] < 0) {
            continue; // Skip negative numbers
        }
        cout << "arr[" << i << "] = " << arr[i] << endl;
    }

    return 0;
}

Output

arr[0] = 1
arr[2] = 3
arr[4] = 5

Explanation

  • The for loop iterates over each element of the array.
  • The condition arr[i] < 0 checks if the current element is negative.
  • If the element is negative, the continue statement is executed, skipping the rest of the loop body for that iteration.
  • The value of the non-negative elements is printed.

Conclusion

The continue statement is a powerful control flow tool in C++ that allows you to skip the rest of the code inside a loop for the current iteration and immediately proceed to the next iteration. It provides greater control over the flow of your program, especially when you need to skip specific conditions within a loop. This chapter covered the use of the continue statement in for, while, and do-while loops, with simple examples and their outputs. Understanding how to use the continue statement effectively will help you write more flexible and readable code. In the next chapter, we will explore the goto statement in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top