C++ Break and Continue Statement

The break and continue statements are used to control the flow of a loop in C++. The break statement is used to exit a loop early, while the continue statement is used to skip the current iteration of a loop and move on to the next one.

Break Statement

The break statement is used to exit a loop early. When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes after the loop.

Here’s an example of using a break statement to exit a for loop early:

#include <iostream>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 5) {
            break;
        }
        std::cout << i << std::endl;
    }
}

In this example, the loop iterates from 1 to 10. However, when the value of i becomes 5, the if statement is true and the break statement is executed. This causes the loop to exit early and the program control resumes after the loop. The output of this program will be the numbers from 1 to 4.

Continue Statement

The continue statement is used to skip the current iteration of a loop and move on to the next one. When the continue statement is encountered inside a loop, the current iteration of the loop is immediately terminated and the program control resumes at the next iteration of the loop.

Here’s an example of using a continue statement to skip the current iteration of a for loop:

#include <iostream>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue;
        }
        std::cout << i << std::endl;
    }
}

In this example, the loop iterates from 1 to 10. However, when the value of i is even, the if statement is true and the continue statement is executed. This causes the current iteration of the loop to be skipped and the program control resumes at the next iteration of the loop. The output of this program will be the odd numbers from 1 to 10.

Break and Continue in nested loops

Break and Continue statement can also be used in nested loops. In this case, the statement will only break or continue the innermost loop it is used in.

Here’s an example of using a break statement in nested loops:

#include <iostream>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break;
}
std::cout << "i: " << i << " j: " << j << std::endl;
}
}
}

In this example, we have two nested loops: an outer for loop that iterates from 1 to 3 and an inner for loop that also iterates from 1 to 3. Inside the inner loop, we have an if statement that checks if the current values of i and j are equal to 2 and 2 respectively. If this condition is true, the break statement is executed, causing the inner loop to exit early. The output of this program will be the following:

i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
i: 2 j: 1

As you can see, the inner loop exits early when the values of i and j become 2 and 2, respectively. The program control then resumes at the next iteration of the outer loop.

Here’s an example of using a continue statement in nested loops:

#include <iostream>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
continue;
}
std::cout << "i: " << i << " j: " << j << std::endl;
}
}
}

In this example, we have the same nested loops as in the previous example. However, this time we are using a continue statement instead of a break statement. Inside the inner loop, we have an if statement that checks if the current values of i and j are equal to 2 and 2 respectively. If this condition is true, the continue statement is executed, causing the current iteration of the inner loop to be skipped. The output of this program will be the following:

i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
i: 2 j: 1
i: 2 j: 3
i: 3 j: 1
i: 3 j: 2
i: 3 j: 3

As you can see, when the values of i and j become 2 and 2, respectively, the current iteration of the inner loop is skipped. The program control then resumes at the next iteration of the inner loop.

In conclusion, the break and continue statements are useful tools for controlling the flow of loops in C++. The break statement allows you to exit a loop early, while the continue statement allows you to skip the current iteration of a loop and move on to the next one. These statements can be used in both single and nested loops, and can be used in conjunction with conditional statements to control the flow of your program more precisely.

It’s important to note that when using these statements, you should be mindful of the scope of the loop they are used in and their potential impact on the overall program flow. Misusing these statements can lead to unexpected behavior and errors in your code. However, when used correctly, they can greatly simplify and improve the readability of your code.

Leave a Reply

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