Continue the loop in C++


With the continue statement we can stop the current iteration of the loop, and continue with the next.

When C++ hits continue, it skips (not execute) any code left, and jumps directly to the next iteration instead.

In this example, we will not let anyone inside a bar if their age is less than 21:

vector<int> ages = {10, 30, 21, 19, 25};

for (int age : ages) {
	if (age < 21) {
		continue;
	}
	cout << "Someone of age " << age << " entered the bar.\n";
}

The output of this program is:

Someone of age 30 entered the bar.
Someone of age 21 entered the bar.
Someone of age 25 entered the bar.

As you can see, our program doesn't print for ages 10 and 19.

Here's what happens during this loop:

1. First iteration:
	a. age = 10
	b. Is age < 21? Yes:
	    continue => Go directly to the next age
	
2. Second iteration:
	a. age = 30
	b. Is age < 21? No.
	c. cout
	
3. Third iteration:
	a. age = 21
	b. Is age < 21? No.
	c. cout

4. Forth iteration:
	a. age = 19
	b. Is age < 21? Yes:
	    continue => Go directly to the next age
      
5. Fifth iteration:
	a. age = 25
	b. Is age < 21? No.
	c. cout

Assignment
Let's allow only the people with height at least 170 into the arena.


Hint
Look at the examples above if you get stuck.


Introduction

The continue statement in C++ is a control flow statement that allows you to skip the current iteration of a loop and proceed to the next iteration. This can be particularly useful in scenarios where you want to skip certain conditions or values within a loop without breaking out of the loop entirely.

Understanding how to use the continue statement effectively can help you write cleaner and more efficient code, especially in cases where you need to filter out specific values or conditions.

Understanding the Basics

The fundamental concept of the continue statement is straightforward: when the continue statement is encountered, the loop immediately jumps to the next iteration, skipping any code that follows it within the loop body.

Consider the following simple example:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue; // Skip even numbers
    }
    cout << i << " ";
}
// Output: 1 3 5 7 9

In this example, the loop prints only the odd numbers between 0 and 9 by skipping the even numbers using the continue statement.

Main Concepts

The key concept behind the continue statement is its ability to control the flow of loops. It is often used in conjunction with conditional statements to filter out specific values or conditions.

Let's break down the example provided in the problem statement:

vector<int> ages = {10, 30, 21, 19, 25};

for (int age : ages) {
    if (age < 21) {
        continue; // Skip ages less than 21
    }
    cout << "Someone of age " << age << " entered the bar.\n";
}

In this example, the loop iterates over a vector of ages. If the age is less than 21, the continue statement is executed, causing the loop to skip the current iteration and move to the next age. This ensures that only ages 21 and above are printed.

Examples and Use Cases

Let's consider another example where we want to allow only people with a height of at least 170 cm into an arena:

vector<int> heights = {160, 175, 168, 180, 172};

for (int height : heights) {
    if (height < 170) {
        continue; // Skip heights less than 170
    }
    cout << "Someone with height " << height << " entered the arena.\n";
}

The output of this program will be:

Someone with height 175 entered the arena.
Someone with height 180 entered the arena.
Someone with height 172 entered the arena.

In this case, the loop skips heights less than 170 and prints only the heights that meet the condition.

Common Pitfalls and Best Practices

One common mistake when using the continue statement is placing it in the wrong part of the loop, which can lead to unintended behavior. Always ensure that the continue statement is placed correctly within the conditional block.

Best practices for using the continue statement include:

Advanced Techniques

In more advanced scenarios, the continue statement can be used in nested loops or with more complex conditions. For example, you might use it to skip certain combinations of values in a nested loop:

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (i == j) {
            continue; // Skip when i equals j
        }
        cout << "(" << i << ", " << j << ") ";
    }
    cout << endl;
}

This example skips the pairs where the values of i and j are equal.

Code Implementation

Let's implement the assignment where we allow only people with a height of at least 170 cm into the arena:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> heights = {160, 175, 168, 180, 172};

    for (int height : heights) {
        if (height < 170) {
            continue; // Skip heights less than 170
        }
        cout << "Someone with height " << height << " entered the arena.\n";
    }

    return 0;
}

This code iterates over a vector of heights and uses the continue statement to skip heights less than 170, printing only the heights that meet the condition.

Debugging and Testing

When debugging code that uses the continue statement, it's important to ensure that the conditions for skipping iterations are correctly defined. Use print statements or a debugger to verify that the loop behaves as expected.

To test the function, you can create various test cases with different height values and verify that the output matches the expected results.

Thinking and Problem-Solving Tips

When approaching problems that involve the continue statement, consider the following strategies:

Conclusion

Mastering the use of the continue statement in C++ can help you write more efficient and readable code by allowing you to skip specific iterations in a loop. By understanding the basics, common pitfalls, and best practices, you can effectively use the continue statement in various programming scenarios.

Practice using the continue statement in different contexts to become more comfortable with its application and to improve your problem-solving skills.

Additional Resources