DEV Community

Cover image for Jump Statements in Java
ABISHEK M
ABISHEK M

Posted on

Jump Statements in Java

1. What are Jump Statements?

Jump statements are used to change the normal flow of program execution.

Normally, Java executes statements one by one. But sometimes, we need to:

  • Stop a loop immediately.
  • Skip the current iteration of a loop.
  • Exit from a method.
  • Return a value from a method.

Java provides three main jump statements:

  1. break
  2. continue
  3. return

2. Break Statement

The break statement is used to terminate a loop or a switch statement immediately.

When Java encounters break, it exits the nearest enclosing loop or switch.

Syntax

break;
Enter fullscreen mode Exit fullscreen mode

Example: Break in a for loop

public class Main {
    public static void main(String[] args) {

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

            if (i == 3) {
                break;
            }

            System.out.println(i);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

1
2
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. Initially, i = 1.
  2. The condition i == 3 is false.
  3. Java prints 1.
  4. The loop continues and prints 2.
  5. When i = 3, the condition becomes true.
  6. The break statement executes.
  7. The loop terminates immediately.

Important: When break executes, the remaining iterations of the loop are skipped.

Break in a while loop

int i = 1;

while (i <= 5) {

    if (i == 4) {
        break;
    }

    System.out.println(i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
Enter fullscreen mode Exit fullscreen mode

When i becomes 4, the loop stops.

Break in a switch statement

The break statement is also commonly used in switch statements.

int day = 2;

switch (day) {

    case 1:
        System.out.println("Monday");
        break;

    case 2:
        System.out.println("Tuesday");
        break;

    case 3:
        System.out.println("Wednesday");
        break;

    default:
        System.out.println("Invalid day");
}
Enter fullscreen mode Exit fullscreen mode

Output:

Tuesday
Enter fullscreen mode Exit fullscreen mode

Here, break prevents execution from continuing into the following cases.


3. Continue Statement

The continue statement is used to skip the current iteration of a loop.

Unlike break, it does not terminate the entire loop. Instead, execution moves to the next iteration.

Syntax

continue;
Enter fullscreen mode Exit fullscreen mode

Example: Continue in a for loop

public class Main {
    public static void main(String[] args) {

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

            if (i == 3) {
                continue;
            }

            System.out.println(i);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

1
2
4
5
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. When i = 1, Java prints 1.
  2. When i = 2, Java prints 2.
  3. When i = 3, the condition becomes true.
  4. The continue statement skips the remaining statements in that iteration.
  5. The loop moves to the next iteration.
  6. Java prints 4 and 5.

Important: continue skips only the current iteration. The loop continues running.

Continue in a while loop

int i = 0;

while (i < 5) {

    i++;

    if (i == 3) {
        continue;
    }

    System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
4
5
Enter fullscreen mode Exit fullscreen mode

Here, when i becomes 3, the continue statement skips the print statement.

Note: In a while loop, make sure the loop variable is updated before continue when necessary. Otherwise, the loop may become infinite.


4. Difference Between Break and Continue

Both statements change the normal execution of loops, but their purposes are different.

Break Continue
Terminates the entire loop. Skips the current iteration.
Control moves outside the loop. Control moves to the next iteration.
Remaining iterations are not executed. Remaining iterations continue normally.
Can be used in loops and switch statements. Used in loops.

Simple Example

Using break:

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

    if (i == 3) {
        break;
    }

    System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
Enter fullscreen mode Exit fullscreen mode

Using continue:

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

    if (i == 3) {
        continue;
    }

    System.out.println(i);
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
4
5
Enter fullscreen mode Exit fullscreen mode

5. Return Statement

The return statement is used to exit from a method.

It can also send a value back to the place where the method was called.

There are two common uses of the return statement:

  1. Return without a value.
  2. Return with a value.

5.1 Return Without a Value

A method with the void return type does not return a value.

We can use return; to exit that method.

Example

public class Main {

    static void display() {

        System.out.println("Hello Java");

        return;

        // Method exits here.
    }

    public static void main(String[] args) {

        display();

        System.out.println("Welcome");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello Java
Welcome
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. The main() method calls display().
  2. The display() method prints Hello Java.
  3. The return statement exits the display() method.
  4. Execution continues from the place where display() was called.
  5. The main() method prints Welcome.

Important: In a void method, return; is optional at the end of the method.

5.2 Return With a Value

A method can return a value to the calling method.

The return type of the method determines what type of value it should return.

Example

public class Main {

    static int add() {

        int a = 10;
        int b = 20;

        return a + b;
    }

    public static void main(String[] args) {

        int result = add();

        System.out.println(result);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

Explanation

  • static int add() means the method returns an integer.
  • return a + b; calculates the sum and sends the result back.
  • int result = add(); stores the returned value.
  • System.out.println(result); prints 30.

Different Return Types

Java methods can return different types of values.

Integer return type:

static int getNumber() {
    return 100;
}
Enter fullscreen mode Exit fullscreen mode

String return type:

static String getName() {
    return "Java";
}
Enter fullscreen mode Exit fullscreen mode

Double return type:

static double getPrice() {
    return 99.50;
}
Enter fullscreen mode Exit fullscreen mode

Boolean return type:

static boolean isPassed() {
    return true;
}
Enter fullscreen mode Exit fullscreen mode

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


6. Multiple Return Statements in a Method

A method can contain multiple return statements.

However, once one return statement executes, the method exits immediately.

Example

public class Main {

    static int checkNumber() {

        int number = 10;

        if (number > 5) {
            return 100;
        }

        return 200;
    }

    public static void main(String[] args) {

        System.out.println(checkNumber());
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

100
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. The variable number stores 10.
  2. Java checks whether number > 5.
  3. The condition is true.
  4. return 100 executes.
  5. The method exits immediately.
  6. return 200 is never reached.

Important: Only the first reached return statement executes.


7. Difference Between Break and Return

Break Return
Exits a loop or switch. Exits the current method.
Does not return a value. Can return a value.
Execution continues after the loop or switch. Execution continues from the method caller.
Used in loops and switch statements. Used inside methods.

Example

Break:

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

    if (i == 3) {
        break;
    }

    System.out.println(i);
}

System.out.println("Loop completed");
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
Loop completed
Enter fullscreen mode Exit fullscreen mode

Return:

static void display() {

    System.out.println("Hello");

    return;

    // Method exits here.
}
Enter fullscreen mode Exit fullscreen mode

Here, the display() method exits when return executes.


8. Important Points to Remember

  1. Java has three main jump statements: break, continue, and return.
  2. break terminates the nearest enclosing loop or switch.
  3. continue skips the current iteration and moves to the next iteration.
  4. return exits the current method.
  5. A void method does not return a value.
  6. A method with a return type such as int, String, or double must return a compatible value on every possible path where it completes normally.
  7. A method can contain multiple return statements.
  8. Once a return statement executes, the method exits immediately.
  9. break and continue are commonly used with looping statements.
  10. return is commonly used when a method needs to send a result back to its caller.

9. Conclusion

Jump statements are important in Java because they help control the execution flow of a program.

  • break: Stops a loop or switch statement.
  • continue: Skips the current loop iteration.
  • return: Exits a method and can return a value.

Understanding these three statements helps us write programs with better control over execution.

Top comments (0)