Table of contents
1.
Introduction
2.
1. Break Statement
2.1.
Using Break Statement to exit a loop
2.2.
java
2.3.
java
2.4.
Use Break as a form of goto
2.5.
java
2.6.
Use Break In a Switch case
2.7.
Java
3.
2. Continue Statement
3.1.
java
4.
3. Return Statement
4.1.
java
5.
Java program to show the working of break, continue and return statement
5.1.
java
6.
Frequently Asked Questions
6.1.
Which is not a valid jump statement in Java?
6.2.
Which jump statement is used to terminate the program?
6.3.
What is the difference between Goto statement and break statement?
6.4.
What happens when a break statement is used inside a nested loop?
6.5.
What is the difference between break and return in Java?
7.
Conclusion
Last Updated: Dec 16, 2024
Medium

Jump Statements in Java

Author Yashesvinee V
1 upvote

Introduction

The execution of a Java program is done in sequential order. Looping statements like for, while, and do-while are used to execute a set of statements repeatedly. If such an execution were to continue, it would go on forever. This can be prevented by adding appropriate terminating conditions to the loop or by implementing jump statements. 

Jump Statements in Java

A jump statement in Java helps transfer the control of a program from one line of the code to another and skip the ones in between.

Jumping statements are control statements that move program execution from one location to another. Jump statements are used to shift program control unconditionally from one location to another within the program. Jump statements are typically used to abruptly end a loop or switch-case. 

Types of Jump Statements in Java

  1. Break Statement
  2. Continue Statement
  3. Return Statement

1. Break Statement

Break statements in Java is used in three ways:

Using Break Statement to exit a loop

In Java, the break statement is used to exit loops prematurely, similar to how it's used in other programming languages. You can use it within for, while, and do-while loops to stop the loop's execution when a specific condition is met. The syntax for using the break statement in Java is as follows:

while loop:

  • java

java

while (condition) {
    // Code inside the loop
    if (condition) {
        break;
        // break keyword
    }
    // Code continues inside the loop
}

// Code after the loop
You can also try this code with Online Java Compiler
Run Code

for loop:

  • java

java

for (int i = 0; i < limit; i++) {
    // Code inside the loop
    if (condition) {
        break;
        // break keyword
    }
    // Code continues inside the loop
}

// Code after the loop
You can also try this code with Online Java Compiler
Run Code

Use Break as a form of goto

In Java, the break statement is not directly used as a form of goto. The break statement is specifically designed to exit from loops prematurely, and it cannot be used to jump to arbitrary locations in the code like a traditional goto statement.

The goto statement is unavailable in Java because it can lead to confusing and hard-to-maintain code. Instead, Java encourages using structured programming constructs like loops, conditional statements, and methods to control the flow of the program.

  • java

java

boolean conditionMatch = false;

while (Condition) {
    // Code inside the loop
    if (conditionToBreak) {
        conditionMatch = true;
        break;
    }
    // Code continues inside the loop
}

// Code after the loop
if (conditionMatch ) {
    // Code to be executed after the "goto" point
}
You can also try this code with Online Java Compiler
Run Code

Use Break In a Switch case

In Java, we can use the break statement within a switch case to exit the switch block and prevent fall-through to subsequent cases. Here's an example:

  • Java

Java

class Main {
public static void main(String[] args) {
int day = 3;

switch (day) {
case 1:
System.out.println("Monday");
break; // Exit switch after executing case 1
case 2:
System.out.println("Tuesday");
break; // Exit switch after executing case 2
case 3:
System.out.println("Wednesday");
break; // Exit switch after executing case 3
case 4:
System.out.println("Thursday");
break; // Exit switch after executing case 4
case 5:
System.out.println("Friday");
break; // Exit switch after executing case 5
default:
System.out.println("Weekend");
break; // Exit switch for any other value
}
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Wednesday

 

Explanation:

In this example, the break statement is used after each case to exit the switch block. This prevents the flow from falling through to subsequent cases. If day is, for example, 3, only "Wednesday" will be printed, and the switch statement will terminate after that case.

2. Continue Statement

Continue is a jump statement used when a loop’s current iteration is to be skipped. It allows the control to temporarily exit the loop and ignore the remaining statements that follow. After this jump, the control immediately moves with the next iteration of the same loop.

  • java

java

public class Main 
{
  public static void main(String args[])
  {
      for (int i = 10; i > 0 ;i--)
      {
          if (i%2 == 0)
          continue;
          System.out.print(i+ "  ");
      }
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

9  7  5  3  1

 

Explanation:

This Java code prints odd numbers in reverse order from 9 to 1 using a for loop. The continue statement skips even numbers.

3. Return Statement

The return keyword is used to transfer a program’s control from a method back to the one that called it. Since the control jumps from one part of the program to another, return is also a jump statement.

It can be in Two forms:

1. Return with a value

return expression;
Where expression is the value or result that the function is returning.

2. Return without a value

return; 
It is without an accompanying expression.

  • java

java

public class Main
{
    public static int add(int a, int b){
        int sum = a+b;
        return sum;
    }
    public static void main(String[] args) {
        int x = 5, y = 10;
        int sum = add(x, y);
        System.out.println("Sum of a and b: " + sum);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Sum of a and b: 15

 

Explanation:

This Java code defines a program with a function add() that calculates the sum of two integers. In the main method, it initializes x and y, calls add, and prints the sum.

Try it by yourself on online java compiler.

Java program to show the working of break, continue and return statement

  • java

java

public class BreakContinueReturnDemo {
   public static void main(String[] args) {
       // Using break statement in a loop
       System.out.println("Using break statement in a loop:");
       for (int i = 1; i <= 5; i++) {
           if (i == 3) {
               break;
           }
           System.out.println("Current value: " + i);
       }
       // Using continue statement in a loop
       System.out.println("\nUsing continue statement in a loop:");
       for (int i = 1; i <= 5; i++) {
           if (i == 3) {
               continue;
           }
           System.out.println("Current value: " + i);
       }
       // Using return statement in a method
       System.out.println("\nUsing return statement in a method:");
       int result = addNumbers(5, 10);
       System.out.println("Result of addition: " + result);
   }
   // Method to demonstrate the use of return statement
   public static int addNumbers(int a, int b) {
       int sum = a + b;
       return sum;
       // Code after the return statement won't be executed
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Using break statement in a loop:
Current value: 1
Current value: 2
Using continue statement in a loop:
Current value: 1
Current value: 2
Current value: 4
Current value: 5
Using return statement in a method:
Result of addition: 15

Frequently Asked Questions

Which is not a valid jump statement in Java?

Jump statements in Java: `break,` `continue,` and `goto` (unused). `return` is not a jump statement; it exits a method and returns a value to the caller.

Which jump statement is used to terminate the program?

In Java, the `System.exit()` method terminates the program. It is not a jump statement but a method that forces the program to exit with a specified status code.

What is the difference between Goto statement and break statement?

The goto statement is used to transfer control unconditionally to another labeled part of the program, while the break statement is used to exit from loops or switch statements prematurely based on a certain condition.

What happens when a break statement is used inside a nested loop?

A break statement inside a nested loop terminates only the innermost loop in which it is placed, resuming execution after that loop. Outer loops continue execution unless explicitly interrupted.

What is the difference between break and return in Java?

break exits a loop or switch statement, resuming execution immediately after it. return exits the entire method, optionally passing a value to the caller, halting further method execution.

Conclusion

Jump statements in Java, such as break, continue, and return, are essential tools for controlling the flow of execution within loops and methods. They help manage program logic efficiently by allowing you to exit or skip parts of code based on specific conditions. Understanding how and when to use these statements can lead to cleaner, more readable, and optimized code. This article extensively discusses the break and continue statements in Java and their implementation with examples. 

Related Links:

Live masterclass