0Pricing
C++ Academy · Lesson

Early Returns break continue and goto

Refactor nested conditionals with early returns and use break, continue and goto judiciously.

Early Returns break continue and goto is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Pyramid of Doom

Deeply nested conditionals are hard to read. Early returns flatten the structure dramatically.

// Pyramid
int process(int n) {
    if (n >= 0) {
        if (n < 100) {
            if (isValid(n)) {
                return compute(n);
            }
        }
    }
    return -1;
}

Refactor with Early Returns

Bail out as soon as a precondition fails. Each guard clause is a flat one-liner.

int process(int n) {
    if (n < 0)      return -1;
    if (n >= 100)   return -1;
    if (!isValid(n)) return -1;
    return compute(n);
}

Guard Clauses

A guard clause rejects invalid input at the top of a function. It makes the happy path obvious.

break in Loops

Exit a loop when you find what you are looking for, or when an error makes continuing pointless.

continue: Skip the Rest

Use continue to skip cases that do not apply, without nesting the remainder of the body.

for (auto& item : items) {
    if (item.skipped) continue;
    process(item);
}

Breaking Out of Nested Loops

break only exits one level. For multi-level escape, use a flag, refactor into a function, or use goto as a last resort.

bool found = false;
for (int i = 0; i < n && !found; ++i) {
    for (int j = 0; j < m; ++j) {
        if (grid[i][j] == target) {
            found = true;
            break;
        }
    }
}

The goto Statement

C++ inherits goto from C. Modern code rarely needs it — but it has one legitimate use: jumping out of nested loops or error paths.

for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
        if (error_condition) goto cleanup;
    }
}

cleanup:
free_resources();

Why goto Is Discouraged

Arbitrary jumps make control flow hard to follow. Reach for refactoring or flags first. Reserve goto for rare cleanup paths in low-level code.

Refactoring goto Away

Most goto uses can be replaced by:

  • Extracting the inner block into a function
  • Using a flag variable
  • Throwing and catching an exception

return in destructors

You cannot return early from a destructor with return; at the top — destructors do not return values. But you can structure them with early conditions to skip work.

The Single-Exit Style (Old)

Older code sometimes used a single return at the bottom and a result variable. Modern C++ favors multiple early returns for clarity. RAII handles cleanup automatically.

Quick Check

Which control flow technique most cleanly replaces nested if-else chains in a function?

Recap

Use early returns as guard clauses, break and continue sparingly for clear intent, and goto only for rare nested-loop or cleanup escapes. The aim is readable, flat control flow.

Frequently asked questions

Is the “Early Returns break continue and goto” lesson free?

Yes — the full text of “Early Returns break continue and goto” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Early Returns break continue and goto”?

Refactor nested conditionals with early returns and use break, continue and goto judiciously. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Early Returns break continue and goto” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Branching: if-else, ternary, switch-case
  2. Loop Patterns: for, while, do-while
  3. Range-Based for Loop with Containers
  4. Early Returns break continue and goto
← Back to C++ Academy