WikiGalaxy

Personalize

C++ Break Statement

Concept Overview:

The break statement is used in C++ to exit a loop or switch statement prematurely. It is commonly used to terminate a loop when a certain condition is met, bypassing the normal loop termination condition.

Use in Loops:

Within loops, break helps in stopping the iteration as soon as a specific condition is true, which can optimize the performance by avoiding unnecessary iterations.


#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break;
        }
        cout << i << " ";
    }
    return 0;
}
    

Explanation:

In this example, the loop prints numbers from 0 to 4. Once i reaches 5, the break statement is executed, terminating the loop.

Console Output:

0 1 2 3 4

C++ Continue Statement

Concept Overview:

The continue statement skips the current iteration of a loop and proceeds with the next iteration. It is useful when you want to skip certain elements or conditions within a loop without terminating it.

Use in Loops:

In loops, continue is often used to skip over unwanted values or to optimize the loop by avoiding unnecessary operations for specific conditions.


#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            continue;
        }
        cout << i << " ";
    }
    return 0;
}
    

Explanation:

In this example, the loop prints only odd numbers. The continue statement skips the even numbers by continuing to the next iteration when i % 2 == 0 is true.

Console Output:

1 3 5 7 9

Using Break in Nested Loops

Concept Overview:

In nested loops, a break statement only exits the loop in which it is placed. This behavior is crucial when working with multi-level iterations, where control needs to be carefully managed.


#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (j == 2) {
                break;
            }
            cout << "(" << i << ", " << j << ") ";
        }
    }
    return 0;
}
    

Explanation:

In this nested loop example, the inner loop breaks when j equals 2, thus printing pairs until (i, 1). The outer loop continues unaffected by the inner loop's break.

Console Output:

(0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)

Continue in Nested Loops

Concept Overview:

When using continue in nested loops, it only affects the loop in which it is placed. This allows skipping specific iterations of the inner loop while maintaining the flow of the outer loop.


#include <iostream>
using namespace std;

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

Explanation:

Here, the continue statement skips the inner loop's iteration when j equals 1. The outer loop remains unaffected, continuing its iterations.

Console Output:

(0, 0) (0, 2) (1, 0) (1, 2) (2, 0) (2, 2)

Break in Switch Statements

Concept Overview:

The break statement is essential in switch statements to prevent fall-through. Without it, all subsequent cases are executed after the matched case.


#include <iostream>
using namespace std;

int main() {
    int num = 2;
    switch (num) {
        case 1:
            cout << "One";
            break;
        case 2:
            cout << "Two";
            break;
        default:
            cout << "Default";
    }
    return 0;
}
    

Explanation:

In this switch statement, the break prevents fall-through after the case 2, ensuring only "Two" is printed when num equals 2.

Console Output:

Two

Continue with While Loop

Concept Overview:

The continue statement in a while loop skips the rest of the loop body and checks the condition again, making it efficient for skipping specific iterations.


#include <iostream>
using namespace std;

int main() {
    int i = 0;
    while (i < 5) {
        i++;
        if (i == 3) {
            continue;
        }
        cout << i << " ";
    }
    return 0;
}
    

Explanation:

In this while loop, the continue statement skips printing the number 3, continuing with the next iteration of the loop.

Console Output:

1 2 4 5

Break in Do-While Loop

Concept Overview:

The break statement in a do-while loop can be used to exit the loop before the condition is evaluated again, providing control over loop termination.


#include <iostream>
using namespace std;

int main() {
    int i = 0;
    do {
        cout << i << " ";
        if (i == 2) {
            break;
        }
        i++;
    } while (i < 5);
    return 0;
}
    

Explanation:

In this do-while loop, the break statement exits the loop when i equals 2, demonstrating early termination in a do-while loop.

Console Output:

0 1 2

Continue in Do-While Loop

Concept Overview:

Using continue in a do-while loop skips the remaining statements in the loop body and proceeds to evaluate the condition again, allowing selective iteration.


#include <iostream>
using namespace std;

int main() {
    int i = 0;
    do {
        i++;
        if (i == 3) {
            continue;
        }
        cout << i << " ";
    } while (i < 5);
    return 0;
}
    

Explanation:

In this do-while loop, the continue statement skips the print statement when i equals 3, demonstrating its use in a do-while loop.

Console Output:

1 2 4 5

Break with Labels

Concept Overview:

In C++, labeled statements can be used with break to exit multiple levels of nested loops, providing more control over complex loop structures.


#include <iostream>
using namespace std;

int main() {
    int i = 0, j = 0;
    outer:
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (i + j == 3) {
                break outer;
            }
            cout << "(" << i << ", " << j << ") ";
        }
    }
    return 0;
}
    

Explanation:

In this example, the break outer statement exits both loops when the sum of i and j equals 3, showcasing the power of labeled breaks.

Console Output:

(0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (2, 0)

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025