Open In App

Jump Statements in Programming

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Jump statements in programming allow altering the normal flow of control within a program. These statements provide a way to transfer the execution to a different part of the code, facilitating conditional exits, loop control, function return, or unconditional jumps.

What is a Jump Statement?

A jump statement is a control flow statement in programming that alters the normal sequential execution of code by transferring program control to a different part of the code. Jump statements include commands like “break,” “continue,” “return,” and “goto,” each serving specific purposes:

  • break: Terminates the current loop iteration or exits a switch statement, transferring control to the statement immediately following the loop or switch block.
  • continue: Skips the rest of the current loop iteration and proceeds to the next iteration of the loop.
  • return: Exits a function prematurely and returns a value, if specified, to the caller.
  • goto: Allows transferring control to a labeled statement within the same function or block, though its usage is discouraged in modern programming due to potential complications in code readability and maintenance.

Break Statement:

The break statement is a control flow statement in programming that is used to terminate the execution of a loop or switch statement prematurely. When encountered, the break statement immediately exits the innermost loop or switch, allowing the program to continue with the next statement after the loop or switch block.

Continue Statement:

In programming, the continue statement is used to alter the flow of control within loops. It is typically employed in iterative structures, such as loops, to skip the remaining code inside the loop for the current iteration and move to the next iteration. The continue statement is useful when certain conditions are met, and the remaining code for the current loop iteration is unnecessary.

Return Statement:

In programming, the return statement is used to terminate the execution of a function and return a value to the calling code. It is a fundamental element in many programming languages, allowing functions to produce results that can be utilized by other parts of a program.

Goto Statement:

In programming, the goto statement is a control flow statement that allows a program to jump to a labeled section of code, providing an unconditional transfer of control. While the goto statement can be useful, it is often considered harmful and can lead to less readable and maintainable code, as its misuse may result in “spaghetti code.”

Jump Statement in C:

C
#include <stdio.h>

int getResult()
{
    int result = 10;
    if (result > 0) {
        return result; // Return a positive value
    }
}

int main()
{
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue; // Skip the remaining code for i=3
        }
        printf("%d ", i);
        if (i == 4) {
            break; // Exit the loop when i=4
        }
    }

    printf("%d ", getResult());

    int num = 5;
    if (num == 5) {
        goto exit; // Unconditionally jump to the 'exit'
                   // label
    }

exit:
    printf("Using goto statement.\n");

    return 0;
}

Output
1 2 4 10 Using goto statement.

Jump Statement in C++:

C++
#include <iostream>
using namespace std;

int getResult()
{
    int result = 10;
    if (result > 0) {
        return result; // Return a positive value
    }
}

int main()
{
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue; // Skip the remaining code for i=3
        }
        cout << i << " ";
        if (i == 4) {
            break; // Exit the loop when i=4
        }
    }

    cout << getResult() << " ";

    int num = 5;
    if (num == 5) {
        goto exitLabel; // Unconditionally jump to the
                        // 'exitLabel' label
    }

exitLabel:
    cout << "Using goto statement." << endl;

    return 0;
}

Output
1 2 4 10 Using goto statement.

Jump Statement in Java:

Java
/*package whatever //do not write package name here */

import java.io.*;

class GFG {
    static int getResult()
    {
        int result = 10;
        if (result > 0) {
            return result; // Return a positive value
        }
        return 0; // Return a default value (not reached in
                  // this example)
    }

    public static void main(String[] args)
    {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                continue; // Skip the remaining code for i=3
            }
            System.out.print(i + " ");
            if (i == 4) {
                break; // Exit the loop when i=4
            }
        }

        System.out.print(getResult() + " ");
        // Java does not support goto
    }
}

Output
1 2 4 10 

Jump Statement in Python:

Python3
def get_result():
    result = 10
    if result > 0:
        return result  # Return a positive value


for i in range(1, 6):
    if i == 3:
        continue  # Skip the remaining code for i=3
    print(i, end=" ")
    if i == 4:
        break  # Exit the loop when i=4

print(get_result(), end=" ")
# Python does not support goto

Output
1 2 4 10 

Jump Statement in C#:

C#
using System;

public class GFG {

    static int GetResult()
    {
        int result = 10;
        if (result > 0) {
            return result; // Return a positive value
        }
        return 0; // Return a default value (not reached in
                  // this example)
    }

    static void Main()
    {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                continue; // Skip the remaining code for i=3
            }
            Console.Write(i + " ");
            if (i == 4) {
                break; // Exit the loop when i=4
            }
        }

        Console.Write(GetResult() + " ");

        int num = 5;
        if (num == 5) {
            goto Exit; // Unconditionally jump to the 'exit'
                       // label
        }

    Exit:
        Console.WriteLine("Using goto statement.");
    }
}

Output
1 2 4 10 Using goto statement.

Jump Statement in Javascript:

Javascript
function getResult() {
    let result = 10;
    if (result > 0) {
        return result; // Return a positive value
    }
    return 0; // Return a default value (not reached in this example)
}

for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue; // Skip the remaining code for i=3
    }
    console.log(i + " ");
    if (i === 4) {
        break; // Exit the loop when i=4
    }
}

console.log(getResult() + " ");
// Javascript does not support goto statement

Output
1 
2 
4 
10 

Conclusion:

Jump statements in programming are like shortcuts that allow the program to jump to different places based on certain conditions. They help control the flow of the program by skipping parts of the code, exiting loops early, or returning values from functions. While they can make code more efficient, using them too much can make the code harder to understand, so it’s best to use them carefully.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads