Jump statements

5 minutes read

Jump statements are an essential part of the C++ programming language, allowing programmers to alter the normal flow of execution within a program. They allow you to jump to a different section of code, bypassing the usual order of execution. You'll find it useful for handling specific conditions, controlling loops, or terminating program execution.

Let's look at the main ways to change the behavior of programs:

  • The goto statement

  • The exit() function

  • The return statement

  • The continue statement

  • The break statement

In this topic, you'll come across functions in the examples. You'll see their explanation in the following topics. You don't need a deep understanding of functions right now, but if you have any difficulties, you can return here after studying the topic of functions.

The goto statement

The goto statement in C++ allows you to transfer control to a labeled statement within the same function. You can use it to implement jumps across different sections of code.

But its usage is generally discouraged due to its potential to create complex and hard-to-maintain code structures.

#include <iostream>

int main() {
    int value;
    std::cout << "Enter a value: ";
    std::cin >> value;

    if (value < 0) {
        goto negative;
    } else {
        goto positive;
    }

negative:
    std::cout << "Negative value entered!" << std::endl;
    return 0;

positive:
    std::cout << "Positive value entered!" << std::endl;
    return 0;
}

Here is the output of our code:

Enter a value: 10
Positive value entered!

And if we enter another value:

Enter a value: -3
Negative value entered!

The return statement

Use the return statement to terminate the execution of a function and return a value to the caller. You can also use it to terminate a program if it is placed in the main function. If you specify the return value, it communicates information to the calling code.

#include <iostream>

int multiply(int a, int b) {
    return a * b; // exit from multiply function
}

int main() {
    int result = multiply(5, 10);
    std::cout << "Multiplication result: " << result << std::endl;
    return 0; // exit from our programm
}

Here is the output of our code:

Multiplication result: 50

The exit() function

The exit() function allows you to terminate the program's execution at any point. It takes an optional integer argument that represents the exit status of the program. A non-zero value conventionally indicates abnormal termination, while a zero value indicates successful execution.

#include <iostream>

void exitProgram() {
    std::cout << "Exiting the program!" << std::endl;
    exit(0);
}

int main() {
    int value;
    std::cout << "Enter a value: ";
    std::cin >> value;

    if (value == 0) {
        exitProgram();
    }

    std::cout << "Continuing program execution..." << std::endl;
    return 0;
}

Here is the output of our code:

Enter a value: 0
Exiting the program!

And if you enter another value:

Enter a value: 42
Continuing program execution...

Note that, unlike return, the exit() function terminates the execution of the application, and does not exit the function.

The continue statement

Use the continue statement within loops to skip the remaining statements within the loop body and move to the next iteration. It allows you to control the flow of a loop based on certain conditions.

You can use this statement inside any kind of loop.

  • inside the for-loop, the continue statement causes control to immediately move to the increment/decrement statement;

  • inside the while or do-while loop, control immediately moves to the condition.

#include <iostream>

int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 2) {
            continue;
        }
        std::cout << i << " ";
    }
    std::cout << std::endl;
    return 0;
}

Here is the output of our code:

0 1 3 4 

The break statement

The break statement has two uses:

  • it terminates the current loop of any type (for, while, do-while);

  • it terminates a case in the switch statement;

The following example demonstrates a loop that includes one break.

#include <iostream>

int main() {
    int sum = 0;

    for (int i = 1; i <= 10; i++) {
        sum += i;
        if (sum > 15) {
            break;
        }
    }

    std::cout << "Sum: " << sum << std::endl;
    return 0;
}

In the code above, the condition to continue the loop is always true, but it will successfully stop when the variable sum > 15 through the use of break inside the conditional statement.

The break statement terminates only the loop in which it is currently located. If this loop is performed inside another loop, the outer loop won't stop.

The following code prints a ladder of numbers.

#include <iostream>

int main() {
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            std::cout << j << " ";
            if (i == j) {
                break;
            }
        }
        std::cout << std::endl;
    }
    return 0;
}

The break statement can't stop the outer loop (with variable i) and the code prints:

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

The continue statement and the break statement only affect the loop in which they are located. The continue statement cannot skip the current iteration of the outer loop.

Conclusion

In conclusion, jump statements in C++ provide programmers with control over the flow of execution within a program. You explored the major aspects of goto, exit, return, continue, and break statements. It's important to use these jump statements judiciously and consider their impact on code readability and maintainability. By understanding and utilizing these jump statements effectively, you can enhance the control and flexibility of your C++ programs.

8 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo