Jump Statements in Java

Introduction

 
In Java jump statements are mainly used to transfer control to another part of our program depending on the conditions. These statements are very useful from the programmer's view because these statements allow alteration of the flow of execution of the program. These statements can be used to jump directly to other statements, skip a specific statement and so on. In Java we have the following three jump statements:
  1. break (simple and labeled)
  2. continue
  3. return

The break statement

 
If we want to go out of a loop then we use a break statement. If we use a break statement in a loop then execution will continue with the immediate next statement outside the loop. After a break, all the remaining statements in the loop are skipped. The break statement can be used in a while loop, for loop, do-while loop, and in a switch case.
 
Syntax
  1. if(condition)  
  2. {  
  3.    break;  
  4. }  
Example
  1. package myclass1;  
  2. class Myclass1  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         int n = 1;  
  7.         while (n <= 15)  
  8.         {  
  9.             System.out.println("\n" + n);  
  10.             n++;  
  11.             if (n == 7)  
  12.             {  
  13.                 break;  
  14.             }  
  15.         }  
  16.     }  
  17. }  
Output
 
breakstatement.jpg 
 

The labeled break statement

 
We can also use the break statement with a label. The break statement is used when we want to transfer the flow of control from an inner loop to an outer loop, but when we want the flow of control to exit the outer loop then we use a labeled break statement. A labeled break statement is somehow similar to the goto statement.
 
Syntax
  1. if(condition)  
  2. break label_name;  
Example
  1. package myclass1;  
  2. class Myclass1  
  3. {  
  4.    public static void main (String args[])  
  5.    {  
  6.       boolean a=true;  
  7.       m:  
  8.       {  
  9.          n:  
  10.          {  
  11.             o:  
  12.             {  
  13.                System.out.println("Before break");  
  14.                if(a)  
  15.                break n;  
  16.                System.out.println("This will not execute");  
  17.             }  
  18.          }  
  19.          System.out.println("After break");  
  20.       }  
  21.    }  
  22. }  
Output
 
return new.jpg 
 

The continue statement

 
The continue keyword is used mainly with loops. When we do not want to execute some statements then we use a continue statement to skip those statements to execute. If the continue statement is confronted in the program then it will start the next iteration. It does not terminate the loop, it just skips some part of the loop. The execution again starts from the top of the loop. In some ways it is similar to the break statement.
 
Syntax
  1. if(condition)  
  2. {  
  3.    continue;  
  4. }  
Example
  1. package myclass1;  
  2. class Myclass1  
  3. {  
  4.   public static void main(String args[])  
  5.   {  
  6.      int [] id = {999888777666555};  
  7.      for(int x : id )  
  8.      {  
  9.         if( x == 777 )  
  10.         {  
  11.           continue;  
  12.         }  
  13.         System.out.print( x );  
  14.         System.out.print("\n");  
  15.      }  
  16.   }  
  17. }  
Output
 
continue statement.jpg 
 

The return statement

 
The return statement is the last jump statement. The return statement is used to end the execution of a specific method and then return a value. When we use a return statement in our program then it sends the program control to the method caller. The data type of the returned value should always be equal to the data type of the method's declared return value.
 
Syntax
  1. if(condition)  
  2. {  
  3.    return;  
  4. }  
Example
  1. package myclass1;  
  2. class Myclass1  
  3. {  
  4.    public static void main(String[] args)  
  5.    {  
  6.       yahoo(true);  
  7.       System.out.println("hi");  
  8.    }  
  9.    public static void yahoo(boolean a)  
  10.    {  
  11.       System.out.println("1");  
  12.       if (a)  
  13.       {  
  14.          return;  
  15.       }  
  16.       System.out.println("2");  
  17.       System.out.println("3");  
  18.    }  
  19. }  
Output
 
return statement.jpg 
 

Summary

 
This article has introduced you to the jump statements in Java. It also covered the syntax and the examples of these jump statements.