C++ Break, Continue & Goto Statements: Precise Loop Control Flow

Jump statements alter the normal sequential flow of execution by transferring control unconditionally to another point in the program.

Comparison: break vs continue

#include <iostream>

int main() {
    std::cout << "--- Testing Break (Stops at 5) ---\n";
    for (int i = 1; i <= 10; ++i) {
        if (i == 5) {
            break; // Exits the loop entirely when i reaches 5
        }
        std::cout << i << " ";
    }
    // Output: 1 2 3 4

    std::cout << "\n\n--- Testing Continue (Skips even numbers) ---\n";
    for (int i = 1; i <= 10; ++i) {
        if (i % 2 == 0) {
            continue; // Skips printing for even numbers
        }
        std::cout << i << " ";
    }
    // Output: 1 3 5 7 9
    std::cout << "\n";

    return 0;
}

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is the key difference between break and continue?

The 'break' statement terminates the entire loop immediately, jumping to the statement following the loop. The 'continue' statement only skips the remainder of the current iteration, immediately jumping to the loop's next update/test step.

Q2. Why is the 'goto' statement heavily discouraged in modern C++?

Goto produces unstructured 'spaghetti code' that bypasses object constructors and destructors, severely disrupting RAII and making code untestable and error-prone. Modern control flow (loops, break, return, exceptions) handles every scenario cleanly.

Explore Related C++ Tutorials & Next Steps

Boost your C++ proficiency by studying these closely connected core modules:

C++ For Loop

Applying break and continue inside loops.

C++ Switch

Understanding the role of break in switch-case blocks.

C++ Functions

Returning early from functions.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!