Jumping Statements in Java

In this tutorial, we will learn about Jumping statements in Java. Jumping statements are also part of control statements in Java. Jumping statements are used to send execution control to a specific statement in Java. Here we will learn three jumping statements in Java.

  • break
  • continue
  • return

break statement: Break statement is used to break the normal flow of execution control. we use break statement normally with switch statements in cases and in loops to break loop based on the specific condition. Always keep in mind we can not use break statement outside the loop or switch statement.

class BreakExample
{
   public static void main(String[] args){
    for(int i=0; i<10; i++){
        if(i==5){
            break;
        }
        System.out.println(i);
     }
    System.out.println("outside of for loop");
   }
}

continue statement: Continue statement with loops. It is so simple when continue statement encountered it simply skip the current iteration of the loop and move to the next iteration. If there is any other statement after continue statement that will not execute. Check this example.

class ContinueExample
     {
     public static void main(String[] args){
       for(int i=1; i<=10; i++){
         if(i==5||i==7){
            continue;
          }
        System.out.println(i);
      }
    }
}

return statement: Return statement we use with methods in java. The main motive of the return statement is to return value to its caller. Check this example.

class ReturnExample
   {
    public static void main(String[] args){
      int result =  new ReturnExample().sum(10,15);
      System.out.println("Result is = "+result);
     }

     int sum(int a,int b){
      return a+b;
     }
}
Spread the love