Topic 17 of 91 3. Control Flow in C++ Interactive Tutorial

Jump Statements in C++: break, continue, goto, and return

14 mins read Last updated: 26 Sep 2026 18,900 views

Direct Answer & Core Concept

When writing a C++ program, instructions normally execute one after another, from top to bottom. However, sometimes you need to change this normal execution flow.

For example, you may want to stop a loop when a particular condition is met, skip an unwanted iteration, exit a function after completing a task, or move execution to a specific location in a function.

C++ provides special instructions called jump statements to handle these situations.

There are four main jump statements in C++:

  • break – Terminates a loop or switch statement.

  • continue – Skips the current loop iteration.

  • goto – Transfers execution to a labeled statement.

  • return – Ends the execution of a function.

In this tutorial, you will learn how jump statements work in C++, their syntax, differences, and practical uses with easy-to-understand programming examples.

Detailed Explanation & Mechanics

What Are Jump Statements in C++?

Jump statements are instructions that change the normal flow of execution in a C++ program.

Normally, a program executes statements sequentially. Jump statements allow you to transfer control to another part of the program or terminate the current execution structure.

For example, consider a loop that prints numbers from 1 to 10. If you want to stop printing when the number reaches 5, you can use the break statement.

Example: Understanding Jump Statements

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 10; i++) {

        if (i == 5) {
            break;
        }

        cout << i << endl;
    }

    return 0;
}

Output:

When the value of i becomes 5, the break statement immediately terminates the loop.

This is a simple example of how jump statements change program execution.

Types of Jump Statements in C++

C++ provides four main jump statements.

Jump statementPurposeCommon use
breakTerminates the nearest loop or switchStop a loop
continueSkips the current loop iterationSkip specific values
gotoTransfers control to a labeled statementSpecial control-flow situations
returnEnds a functionReturn a result or exit a function

Let's understand each statement with its syntax and practical examples.

1. break Statement in C++

The break statement is used to immediately terminate the nearest enclosing loop or switch statement.

Once the break statement executes, the program continues from the first statement after the terminated loop or switch.

It is commonly used when you want to stop a loop after finding a particular value or when a condition is satisfied.

Syntax of break Statement

The break statement does not require any condition. It is usually placed inside an if statement when you want to terminate execution based on a specific condition.

Example 1: Using break in a for Loop

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 10; i++) {

        if (i == 5) {
            break;
        }

        cout << i << endl;
    }

    return 0;
}

Output:

Explanation

  • The loop starts with i = 1.

  • The condition checks whether i is less than or equal to 10.

  • Numbers are printed one by one.

  • When i becomes 5, the if condition becomes true.

  • The break statement terminates the loop immediately.

The numbers 5 to 10 are not printed.

Example 2: Using break in a while Loop

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int i = 1;

    while (i <= 10) {

        if (i == 6) {
            break;
        }

        cout << i << endl;

        i++;
    }

    return 0;
}

Output:

The loop stops when the value of i reaches 6.

Example 3: Using break in a switch Statement

The break statement is also commonly used inside a switch statement.

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int choice = 2;

    switch (choice) {

        case 1:
            cout << "Home";
            break;

        case 2:
            cout << "About Us";
            break;

        case 3:
            cout << "Contact";
            break;

        default:
            cout << "Invalid Choice";
    }

    return 0;
}

Output:

Here, break prevents execution from continuing into the next case.

Practical Uses of break

The break statement is useful when:

  • You find the required item in a search operation.

  • A user selects an exit option from a menu.

  • A specific condition requires a loop to stop.

  • You want to exit a switch case.

  • An operation has reached its required result.

2. continue Statement in C++

The continue statement is used to skip the remaining statements in the current loop iteration.

Unlike break, it does not terminate the entire loop.

After executing continue, the loop proceeds to its next iteration according to the rules of that loop.

It is useful when you want to ignore certain values but continue processing the remaining values.

Syntax of continue Statement

The continue statement is used inside looping statements such as for, while, and do-while.

Example 1: Using continue in a for Loop

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 5; i++) {

        if (i == 3) {
            continue;
        }

        cout << i << endl;
    }

    return 0;
}

Output:

Explanation

  • The loop starts with i = 1.

  • The program checks whether i is equal to 3.

  • When the condition is true, continue skips the remaining statements in that iteration.

  • The loop proceeds with the next value.

Therefore, the number 3 is not printed.

Example 2: Using continue to Print Even Numbers

You can also use continue to skip odd numbers and print only even numbers.

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 10; i++) {

        if (i % 2 != 0) {
            continue;
        }

        cout << i << endl;
    }

    return 0;
}

Output:

Explanation

The modulus operator % checks the remainder after division.

  • Even numbers produce a remainder of 0 when divided by 2.

  • Odd numbers produce a remainder of 1.

  • The continue statement skips the odd numbers.

Example 3: Using continue in a while Loop

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int i = 0;

    while (i < 5) {

        i++;

        if (i == 3) {
            continue;
        }

        cout << i << endl;
    }

    return 0;
}

Output:

Notice that the increment happens before continue. This is important in a while loop because the update must still happen to avoid an infinite loop.

Practical Uses of continue

The continue statement is useful when:

  • Skipping invalid records.

  • Ignoring unwanted values.

  • Printing only even or odd numbers.

  • Filtering data inside loops.

  • Processing only records that meet specific conditions.

3. goto Statement in C++

The goto statement is used to transfer program execution directly to a labeled statement within the same function.

Unlike break and continue, it does not need to be used only with loops or switch statements.

You can use goto to jump forward or backward to a label in the same function, subject to C++ scope and initialization rules.

Syntax of goto Statement

main.cpp
Run Online
goto label_name;

// Other statements

label_name:
    // Statement to execute

Here:

  • goto is the jump keyword.

  • label_name is the name of the destination label.

  • A colon (:) is placed after the label.

Example 1: Using goto Statement

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int number = 10;

    if (number == 10) {
        goto message;
    }

    cout << "This message will be skipped." << endl;

    message:

    cout << "Welcome to C++ Programming!";

    return 0;
}

Output:

main.cpp
Run Online
Welcome to C++ Programming!

Explanation

  • The program declares a variable named number.

  • The if condition checks whether the value is 10.

  • Since the condition is true, goto message transfers control to the message label.

  • The statement between the goto and the label is skipped.

  • The program executes the statement after the label.

Example 2: Using goto to Repeat an Operation

The goto statement can also transfer control backward to repeat a section of code.

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int i = 1;

start:

    if (i <= 5) {

        cout << i << endl;

        i++;

        goto start;
    }

    return 0;
}

Output:

Explanation

  • The variable i starts at 1.

  • The program checks whether i is less than or equal to 5.

  • The current value is displayed.

  • The value of i increases by one.

  • goto start transfers execution back to the start label.

  • When i becomes 6, the condition becomes false and the program exits the block.

This example demonstrates repetition using goto, although a for or while loop is a better choice for this task.

Advantages and Disadvantages of goto

AdvantagesDisadvantages
Can transfer control to a specific labelCan make code difficult to understand
Useful in certain low-level control-flow situationsCan create complicated execution paths
Can exit deeply nested structures in special casesMakes debugging and maintenance harder
Supported by the C++ languageCan make program logic less organized

Important: Avoid using goto in ordinary programming whenever possible. Prefer loops, functions, and structured control statements because they make code easier to read and maintain.

4. return Statement in C++

The return statement is used to terminate the execution of a function.

It can also send a value back to the function that called it.

The return statement is commonly used in functions that calculate results, perform operations, or provide information to another part of a program.

Syntax of return Statement

For a function that returns a value:

For a function that does not return a value:

The value returned must be compatible with the function's declared return type.

Example 1: Using return in a Function

main.cpp
Run Online
#include <iostream>
using namespace std;

int add(int a, int b) {

    return a + b;
}

int main() {

    int result = add(10, 20);

    cout << "Sum: " << result;

    return 0;
}

Output:

Explanation

  • The add() function receives two integer arguments.

  • It calculates their sum.

  • return a + b; sends the result back to main().

  • The returned value is stored in the result variable.

  • cout displays the final result.

Example 2: Using return to Exit a Function Early

The return statement can terminate a function before reaching its final statement.

main.cpp
Run Online
#include <iostream>
using namespace std;

void checkNumber(int number) {

    if (number < 0) {

        cout << "Negative Number";

        return;
    }

    cout << "Positive Number or Zero";
}

int main() {

    checkNumber(-5);

    return 0;
}

Output:

Explanation

When the number is negative, the function displays a message and executes return.

The remaining statements in the function are not executed.

Example 3: Using return 0 in main()

The main() function is the starting point of a C++ program.

When main() returns 0, it normally indicates successful program completion to the operating system.

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    cout << "Program Started" << endl;

    cout << "Program Completed";

    return 0;
}

Output:

main.cpp
Run Online
Program Started
Program Completed

Important: In C++, reaching the closing brace of main() is equivalent to returning 0. However, writing return 0; explicitly is common in beginner tutorials.

Practical Uses of return

The return statement is useful when:

  • Returning calculated values from functions.

  • Ending a function after completing a task.

  • Exiting a function when an invalid condition is detected.

  • Returning status information.

  • Ending the main() function.

Difference Between break, continue, goto, and return in C++

All four jump statements change program execution, but they work differently.

Comparison Table

Featurebreakcontinuegotoreturn
Main purposeStop a loop or switchSkip current loop iterationJump to a labelExit a function
Used in loopsYesYesCan be usedCan exit a function containing a loop
Used in switchYesNoCan jump to a labelCan exit the function
Requires a labelNoNoYesNo
Returns a valueNoNoNoSometimes
Common useStop executionSkip valuesSpecial control transferFinish a function

Simple Example of All Four Jump Statements

main.cpp
Run Online
#include <iostream>
using namespace std;

int calculate(int number) {

    if (number < 0) {
        return 0;
    }

    return number * 2;
}

int main() {

    // break example
    for (int i = 1; i <= 5; i++) {

        if (i == 4) {
            break;
        }

        cout << i << " ";
    }

    cout << endl;

    // continue example
    for (int i = 1; i <= 5; i++) {

        if (i == 3) {
            continue;
        }

        cout << i << " ";
    }

    cout << endl;

    // goto example
    goto message;

    cout << "Skipped" << endl;

message:

    cout << "Goto executed" << endl;

    // return example
    cout << "Result: " << calculate(10) << endl;

    return 0;
}

Output:

main.cpp
Run Online
1 2 3
1 2 4 5
Goto executed
Result: 20

Understanding the Output

  • The first loop stops when i reaches 4 because of break.

  • The second loop skips 3 because of continue.

  • The goto statement transfers control to the message label.

  • The calculate() function returns 20.

  • Finally, return 0 ends the main() function successfully.

Difference Between break and continue in C++

Beginners often confuse break and continue because both are used inside loops.

However, they perform different operations.

Featurebreakcontinue
PurposeTerminates the nearest loopSkips the current iteration
Loop executionStops completelyContinues with the next iteration
Remaining iterationsNot executedExecuted normally
Common useStop when a result is foundSkip unwanted values

Example Using break

main.cpp
Run Online
for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        break;
    }

    cout << i << " ";
}

Output:

Example Using continue

main.cpp
Run Online
for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    cout << i << " ";
}

Output:

Remember: break stops the loop, while continue skips only the current iteration.

How Jump Statements Work in Nested Loops

C++ also allows jump statements to be used inside nested loops.

A nested loop is a loop placed inside another loop.

Example: break in Nested Loops

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 3; i++) {

        for (int j = 1; j <= 3; j++) {

            if (j == 2) {
                break;
            }

            cout << i << " " << j << endl;
        }
    }

    return 0;
}

Output:

The break statement terminates only the nearest enclosing loop, which is the inner loop. The outer loop continues normally.

Example: continue in Nested Loops

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    for (int i = 1; i <= 2; i++) {

        for (int j = 1; j <= 3; j++) {

            if (j == 2) {
                continue;
            }

            cout << i << " " << j << endl;
        }
    }

    return 0;
}

Output:

Here, the inner loop skips the value 2 during each iteration of the outer loop.

Common Mistakes When Using Jump Statements in C++

Beginners often make mistakes when working with jump statements. Understanding these problems can help you write better C++ programs.

1. Confusing break and continue

Incorrect understanding:

  • break skips only one iteration.

  • continue stops the entire loop.

This is incorrect.

Correct understanding:

  • break terminates the nearest enclosing loop or switch.

  • continue skips the current loop iteration.

2. Using continue Outside a Loop

The continue statement can only be used within an iteration statement.

Incorrect:

Correct:

main.cpp
Run Online
for (int i = 1; i <= 5; i++) {

    if (i == 3) {
        continue;
    }

    cout << i << endl;
}

3. Forgetting to Update a Variable After continue in a while Loop

Incorrect:

main.cpp
Run Online
int i = 1;

while (i <= 5) {

    if (i == 3) {
        continue;
    }

    cout << i;

    i++;
}

When i becomes 3, the loop repeatedly executes continue without increasing i. This creates an infinite loop.

Correct:

main.cpp
Run Online
int i = 1;

while (i <= 5) {

    if (i == 3) {
        i++;
        continue;
    }

    cout << i << endl;

    i++;
}

4. Using goto Without a Clear Reason

Excessive use of goto can make a program difficult to read and maintain.

Prefer for, while, if, and function-based solutions.

5. Returning the Wrong Data Type

A function declared to return an integer should return a compatible integer value.

Example:

main.cpp
Run Online
int add(int a, int b) {

    return a + b;
}

Make sure the returned value matches the function's declared return type.

6. Forgetting That break Only Exits the Nearest Loop

When using nested loops, break terminates only the innermost loop that contains it.

If you want to stop multiple nested loops, you need additional control logic, a function return, or another appropriate structured solution.

Best Practices for Using Jump Statements in C++

Follow these simple practices when writing C++ programs:

  • Use break when you need to terminate a loop or switch.

  • Use continue when you want to skip a particular iteration.

  • Use return to finish a function or send a result back.

  • Avoid unnecessary use of goto.

  • Always make sure loop variables are updated correctly.

  • Use meaningful conditions with jump statements.

  • Be careful when using jump statements inside nested loops.

  • Keep functions small and easy to understand.

  • Return values that match the declared function type.

  • Test different conditions to verify program behaviour.

Frequently Asked Questions About Jump Statements in C++

1. What are jump statements in C++?

Jump statements are instructions that change the normal flow of execution in a C++ program. The four main jump statements are break, continue, goto, and return.

2. How many jump statements are there in C++?

C++ has four main jump statements: break, continue, goto, and return.

3. What is the use of break in C++?

The break statement immediately terminates the nearest enclosing loop or switch statement.

4. What is the use of continue in C++?

The continue statement skips the remaining statements in the current loop iteration and moves to the next iteration.

5. What is the use of goto in C++?

The goto statement transfers program execution to a labeled statement within the same function.

6. What is the use of return in C++?

The return statement terminates a function and can return a value to the calling function.

7. What is the difference between break and continue?

break terminates the entire nearest enclosing loop, whereas continue skips only the current iteration.

8. Can break be used outside a loop in C++?

Yes. The break statement can also be used inside a switch statement. It cannot be used freely in an ordinary block or function.

9. Can continue be used in a switch statement?

A continue statement can appear inside a switch nested within a loop. It applies to the nearest enclosing loop, not the switch statement itself.

10. Is goto recommended in C++?

Generally, no. Although C++ supports goto, structured alternatives such as loops, functions, and conditional statements usually produce more readable code.

11. Can return be used inside a loop?

Yes. A return statement can be used inside a loop to terminate the entire function, not just the loop.

12. What happens when return 0 is used in main()?

return 0; normally indicates that the program completed successfully and sends a success status to the operating system.

13. What is the difference between break and return?

break exits the nearest enclosing loop or switch, while return exits the entire function.

14. Can goto jump outside a function?

No. A goto statement cannot transfer control to a label in another function. Its destination must be within the same function.

Runnable Code Example: Jump Statements in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>

struct ResourceTracker {
    std::string name;
    ResourceTracker(std::string n) : name(n) { std::cout << "  Constructed: " << name << "
"; }
    ~ResourceTracker() { std::cout << "  Destructed: " << name << "
"; }
};

int searchNumber(int target) {
    ResourceTracker tracker("SearchScope");
    for (int i = 1; i <= 5; ++i) {
        if (i == target) {
            std::cout << "Target found! Returning from function.
";
            return i; // Automatically destructs 'tracker' before returning
        }
    }
    return -1;
}

int main() {
    std::cout << "Starting search...
";
    int result = searchNumber(3);
    std::cout << "Search result: " << result << "
";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Does return safely invoke destructors of stack variables?

A: Yes. ISO C++ guarantees deterministic stack unwinding: before control exits the function, all local automatic objects are destroyed in reverse order of their creation.
Q

Q: When is goto acceptable in modern C++?

A: In modern C++, `goto` is rarely needed due to exceptions, break/continue, and structured programming. Occasionally, it is used in low-level state machines or breaking out of deeply nested multi-level loops.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Rely on structured control flow instead of goto.
2. Ensure every non-void function path explicitly returns a valid value.
3. Leverage early returns (guard clauses) to simplify deep conditional nesting.