Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Jump Statements in Java?
3.
Types of Jump Statements in Java
3.1.
1. Break Statement
3.1.1.
Using Break Statement to exit a loop
3.2.
java
3.3.
java
3.3.1.
Use Break as a form of goto
3.4.
java
3.4.1.
Use Break In a Switch case
3.5.
Java
3.6.
2. Continue Statement
3.7.
java
3.8.
3. Return Statement
3.9.
java
4.
Java program to show working of break, continue and return statement
4.1.
java
5.
Frequently Asked Questions
5.1.
Which is not a valid jump statement in Java?
5.2.
Which jump statement is used to terminate the program?
5.3.
What is the difference between Goto statement and break statement?
5.4.
What is the purpose of jump statements?
6.
Conclusion
Last Updated: Mar 27, 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.

What are Jump Statements in Java?

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. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Jump Statements in Java

  • Break Statement
  • Continue Statement
  • 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

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

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
}

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
}
}
}

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.

Output:

Wednesday

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+ "  ");
      }
   }
}

Explanation:

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

Output:

9  7  5  3  1

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);
    }
}

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.

Output:

Sum of a and b: 15

 

Try it by yourself on online java compiler.

Java program to show 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
   }
}

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 is the purpose of jump statements?

The purpose of jump statements in programming is to alter the flow of control. They allow breaking out loops, skipping iterations, or moving to specific locations in the code.

Conclusion

This article extensively discusses the break and continue statements in Java and their implementation with examples. We hope that this blog has helped you enhance your knowledge regarding jump statement in java and if you would like to learn more, check out our articles on the Coding Ninjas Library. Do upvote our blog to help other ninjas grow. Happy Coding!

Related Links:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like AmazonAdobeGoogleUberMicrosoft, etc. on Coding Ninjas Studio.

Previous article
Break and Continue Statement in Java
Next article
Break Statement in Java
Live masterclass