What are Java Jump Statements With Program Example

In Java, Jump Statements are control statements that shifts control from one part of the program to another part.  It is primarily used to escape from looping statements, skip certain statements based on conditions, exit from a method and so on. These can interrupt the execution of a loop, switch and methods right away, enabling us to change the flow of program execution.

image 2021 01 02 190721

Java supports break, continue, and return. However, Go To statement can be implemented using label and break statements.

However, Java uses label with break statement or continue statement to get some sort of functionality of go to statement. In this, label works as a target point and break statement considers the label to jump out of that particular target point. [Read brief about go to statement implementation and program example]

Break Statement

A break statement is used to terminates the execution of the loop abruptly and flow of cases in a switch statement. Java doesn’t have Go-to statement as C or C++ does. Interestingly, the Break statement can also work as Go-to using label. 

The break statement can be used:

  • Within a Loop to terminate the loop
  • In Switch Cases to terminate case sequence 
  • As Go-To Label
break statement
Figure: Working of Break Statement in a Loop

Syntax: 

if(condition)  
{  
   break;  
}  

Program:

public class ApplicationExample {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            if (i == 3) {
                System.out.println("Terminating or breaking the loop as i reaches to 3 ! ");
                break;
            }
            System.out.println("Executing " + i + " times. ");
        }
    }
}

Output:

Executing 1 times. 
Executing 2 times. 
Terminating or breaking the loop as i reaches to 3 ! \

Continue Statement

It skips the execution of codes of the loop that comes after it and starts the next iteration. Unlike the break statement, it doesn’t terminate a loop, instead, it ends the current iteration. 

It always comes with a conditional statement in order to avoid unreachable code error. It applies on the loop only.

continue statement 1
Figure : Working of Continue Statement

Syntax:

if(condition)  
{  
   continue;  
}

Program:

public class ApplicationExample {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            if (i <= 5) {
                continue;  //  skipping the line 8 and 9 until value of i is not greater than 5
            }
            System.out.println("Executing " + i + " times. ");
        }
    }
}

Output:

Executing 6 times. 
Executing 7 times. 
Executing 8 times. 
Executing 9 times. 

 

Return Statement

It returns a value from a function or method and transfers the control back to the calling function. In other words, it terminates a method in which it’s encountered and shifts the flow back to the calling function. 

return statement java

Syntax:

if(condition)  
{  
   return;  
} 

Program:

public class ApplicationExample {

    public static void main(String[] args) {
        ApplicationExample example = new ApplicationExample();
        example.printMessage(true);

    }
    private void printMessage(boolean controlVar){
        String message = getMessage(controlVar);
        System.out.println(message);
    }

    private String getMessage(boolean isTrue){
        if(isTrue){
            return "Hi I am true statement";
        }
        return "Hi I am false statement";

    }
}

Output:

Hi I am true statement

 

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

public class JumpStatement
{
  public static void main (String[]args)
  {
    int i = 0;
    boolean x = true;		// Simple break Statement 
    while (i < 5)
      {
  System.out.println ("Break will cause termination of while loop");
  i++;
  break;
      }				// break Statement acts as Go-to Statment 
    first:
    {
    second:{
      third:{
    System.out.println ("I am third block");
    if (x)
      break second;
  }
  System.out.println ("I am second block");
      }
      System.out.println ("I am first block");
    }				// Continue Statement acts as Go-to Statment 
    for (i = 0; i < 5; i++)
      {
  System.out.println ("This is before continue");
  if (i > 3)
    continue;
  System.out.println ("This is after continue");
      }				// Return Statement
    if (x)
      return;
    System.out.println ("I wouldn't be executed");
  }
}

Output:

Break will cause termination of while loop
I am third block
I am first block
This is before continue
This is after continue
This is before continue
This is after continue
This is before continue
This is after continue
This is before continue
This is after continue
This is before continue

 

Leave a Reply