Page Content

Tutorials

What Are The Jump statements In C++ With Code Examples?

Jump statements in C++

Jump statements are a type of control flow statement in C++ that shift control to another area of the code and stop a program’s regular sequential execution. Return, break, continue, and goto are the four main jump statements available in C++.
Below is a description of each:

return Statement

  • The function that is presently running can be stopped and control returned to the place from which it was called using the return statement.
  • Return; (with no value) and return expression; (with a value) are its two possible formats.
  • Functions with a void return type can implicitly return after their final statement or use return; to terminate early.
  • A value must be returned by functions with a non-void return type. The type of the returned value must be one that can be implicitly changed to the return type of the function.
  • An int return type is necessary for the special main() method. The compiler inserts return 0; implicitly if control reaches the conclusion of main without an explicit return statement. Generally, a return value of 0 denotes success, whereas a value that is not zero denotes failure.

Example

#include <iostream>
#include <string> // For std::to_string
// Function demonstrating 'return' with a value
int addNumbers(int a, int b) {
    int sum = a + b;
    return sum; // Returns the calculated sum
}
// Function demonstrating 'return' in a void function and early exit
void processPositiveNumber(int n) {
    if (n <= 0) {
        std::cout << "Number is not positive. Exiting function.\n";
        return; // Exits the void function early
    }
    std::cout << "Processing positive number: " << n << std::endl;
    // Implicit return here if no explicit return statement is present
}
int main() {
    int result = addNumbers(5, 3);
    std::cout << "Sum: " << result << std::endl;
    processPositiveNumber(10);
    processPositiveNumber(-5);
    processPositiveNumber(0);
    return 0;
}

Output

Sum: 8
Processing positive number: 10
Number is not positive. Exiting function.
Number is not positive. Exiting function.

break Statement

  • The closest enclosing while, do-while, for, or switch statement is terminated by the break statement.
  • After the loop or switch is terminated, execution continues at the statement that follows.
  • Preventing “fall-through,” in which execution would continue into consecutive case labels, is essential in switch statements. Only the innermost enclosing loop or switch is impacted by the break.

Example

#include <iostream>
#include <string> // For std::to_string
// Function demonstrating 'break' in a loop
void findFirstEven(int arr[], int size) {
    std::cout << "\n--- Demonstrating 'break' ---\n";
    for (int i = 0; i < size; ++i) {
        if (arr[i] % 2 == 0) {
            std::cout << "Found first even number: " << arr[i] << " at index " << i << std::endl;
            break; // Terminates the 'for' loop immediately
        }
        std::cout << "Checking number: " << arr[i] << std::endl;
    }
    std::cout << "Loop finished or broken.\n";
}
int main() {
    int numbers[] = {1, 3, 5, 8, 9, 11, 12};
    findFirstEven(numbers, 7); // Call the function with an array and its size
    return 0;
}

Output

--- Demonstrating 'break' ---
Checking number: 1
Checking number: 3
Checking number: 5
Found first even number: 8 at index 3
Loop finished or broken.

continue Statement

  • The for, while, or do-while iteration of the closest enclosing loop is ended by the continue statement, which also starts the subsequent iteration right away.
  • Control shifts to the conditional test for while and do-while loops. In a for loop, the condition is executed after the step-expression (increment portion) of the loop.

Example

#include <iostream>
#include <string> // For std::to_string 
// Function demonstrating 'continue' in a loop
void printOddNumbers(int limit) {
    std::cout << "\n--- Demonstrating 'continue' ---\n";
    int i = 1;
    while (i <= limit) {
        if (i % 2 == 0) {
            // If 'i' is even, skip the rest of this iteration and go to the next
            i++; // Must increment manually to prevent infinite loop
            continue; // Proceeds to the next iteration of the 'while' loop
        }
        std::cout << "Odd number: " << i << std::endl;
        i++;
    }
    std::cout << "Finished printing odd numbers.\n";
}
int main() {
    printOddNumbers(10); // Call the function with a limit of 10
    return 0;
}

Output

- -- Demonstrating 'continue' ---
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
Finished printing odd numbers.

goto Statement

  • An unconditional leap from the goto to a labelled statement is provided by the goto statement. An identification followed by a colon (:) is called a label.
  • The goto must be in the same function as its target label.
  • Since goto statements can result in “spaghetti code” that is challenging to read, debug, and alter, they are strongly avoided in contemporary C++ programming. They may, however, be helpful in some, uncommon situations, such breaking out of if statements or deeply nested loops. An initialiser and an exception handler cannot be skipped.

Example

#include <iostream>
// Function demonstrating 'goto' (discouraged usage)
void demonstrateGoto(int value) {
    std::cout << "\n--- Demonstrating 'goto' ---\n";
    if (value < 0) {
        goto error_label; // Jumps unconditionally to 'error_label' if value is negative
    }
    std::cout << "Value is non-negative: " << value << std::endl;
    std::cout << "Normal processing complete.\n";
    return; // Normal exit from the function
error_label: // This is the label, the target of the goto statement
    std::cout << "Error: Negative value detected! Cleanup or error handling.\n";
    // In a real-world scenario, you might put cleanup code here.
}
int main() {
    demonstrateGoto(7);  // Calls the function with a positive value
    demonstrateGoto(-3); // Calls the function with a negative value, triggering the goto
    return 0;
}

Output

--- Demonstrating 'goto' ---
Value is non-negative: 7
Normal processing complete.
--- Demonstrating 'goto' ---
Error: Negative value detected! Cleanup or error handling.

You can also read A Comprehensive Guide For C Jump Statements

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index