๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Java Jump Statements with Examples  


Introduction

  • Jump statements transfer the program's control from one part of the code to another, skipping the lines in between.
  • By default, Java programs execute statements line by line from top to bottom, but jump statements allows us to:
    • Break the usual execution order (e.g., exit a loop early using break).
    • Skip parts of the code (e.g., skip an iteration using continue).
    • Return control to the method caller (e.g., exit a method and optionally return a value using return).
  • Examples of Jump Statements in Java:
    1. break: Stops the loop completely when a condition is true.
    2. continue: Skips the current loop step and moves to the next one
    3. return: Ends the method and sends a result back, if needed.
    These are explained deeply as below:

"break" Statement in Java
  • The break statement is used to exit a loop or a switch statement before it has completed its normal execution.
    • Loops: The break statement can be used to terminate loops (for, while, do-while) prematurely when a specific condition is met.
    • Switch Statements: It is commonly used in switch statements to exit a particular case and prevent the execution of subsequent cases.
  • How it works:
    • The break statement stops the loop or case execution and moves the control to the first statement outside the loop or switch block.
  • Syntax:
    break;
  • Program 1 (Using break in a loop):
    public class BreakExample
    {
        public static void main(String[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                if (i == 5)
                {
                    System.out.println("Loop stopped at: " + i);
                    break; // Exit the loop when i equals 5
                }
                System.out.println("Number: " + i);
            }
        }
    }
    Output:
    Number: 1  
    Number: 2  
    Number: 3  
    Number: 4  
    Loop stopped at: 5
  • Program 2 (Using break in a switch statement):
    public class BreakSwitchExample
    {
        public static void main(String[] args)
        {
            int day = 3;
    
            switch (day)
            {
                case 1:
                    System.out.println("Monday");
                    break;
                case 2:
                    System.out.println("Tuesday");
                    break;
                case 3:
                    System.out.println("Wednesday");
                    break;
                case 4:
                    System.out.println("Thrusday");
                    break;
                case 5:
                    System.out.println("Friday");
                    break;
                case 6:
                    System.out.println("Saturday");
                    break;
                case 7:
                    System.out.println("Sunday");
                    break;
                default:
                    System.out.println("Invalid day");
            }
        }
    }
    Output:
    Wednesday

"continue" Statements in Java
  • The continue statement is used to skip the current iteration of a loop and move to the next iteration without completing the remaining code in the loop for that iteration.
  • It is useful when we want to skip specific conditions and proceed with the rest of the loop.
  • How it works:
    • In Loops:
      • When the continue statement is encountered, the loop immediately jumps to the next iteration.
      • In a for loop, the increment/decrement step is executed next.
      • In a while or do-while loop, the condition is checked again.
  • Syntax:
    continue;
  • Program 1 (Using continue in a Loop):
    public class ContinueExample
    {
        public static void main(String[] args)
        {
            for (int i = 1; i <= 5; i++)
            {
                if (i == 3)
                {
                    System.out.println("Skipping number: " + i);
                    continue; // Skip the rest of the code in this iteration
                }
                System.out.println("Number: " + i);
            }
        }
    }
    Output:
    Number: 1  
    Number: 2  
    Skipping number: 3  
    Number: 4  
    Number: 5
  • Program 2 (Using continue in a while loop):
    public class ContinueWhileExample
    {
        public static void main(String[] args)
        {
            int number = 1;
    
            while (number <= 5)
            {
                if (number == 3)
                {
                    System.out.println("Skipping number: " + number);
                    number++; // Increment the number to avoid an infinite loop
                    continue; // Skip the rest of the code in this iteration
                }
                System.out.println("Number: " + number);
                number++;
            }
        }
    }
    Output:
    Number: 1  
    Number: 2  
    Skipping number: 3  
    Number: 4  
    Number: 5

"return" Statements in Java
  • The return statement is used to exit from a method and optionally send a value back to the method's caller.
  • It is essential for returning a result from a method or terminating the execution of a method before it reaches its end.
  • The usage of the return keyword in Java, categorized into two main cases:
    1. Methods returning a value: The return keyword sends a value back to the caller.
    2. Methods not returning a value (Void methods)
      1. Without return: return stops the method early.
      2. With void: return ends the method without returning a value.
    Click Here to read deep explanation of above usage
    • The usage of the return keyword in Java, categorized into two main cases:
      1. Methods Returning a Value
        • Purpose: These methods return a value to the caller.
        • How it works: You specify the type of value the method will return (e.g. int, String, boolean etc), and the method sends this value back using the return keyword.
        • Example:
          public int addNumbers(int a, int b)
          {
              return a + b; // Returns the sum of a and b
          }
      2. Methods Not Returning a Value (Void Methods)
        • These methods do not return anything to the caller, but they can still use return in two ways:
          1. Method Without Return in a Void Function
            • Purpose: To exit the method early without returning anything.
            • How it works: The return statement is used without a value to stop the method's execution and return control to the caller.
            • Example:
              public void checkAge(int age)
              {
                  if (age < 18)
                  {
                      return; // Exits the method early if age is less than 18
                  }
                  System.out.println("You are an adult.");
              }
          2. Methods With void Return Type
            • Purpose: These methods do not return any data to the caller.
            • How it works: return is simply used to exit the method when no value needs to be returned.
            • Example:
              public void greet()
              {
                  System.out.println("Hello, World!");
                  return; // Stops the method here, no value is returned
              }
  • Syntax:
    return value; // For methods with return types, to send a value back.
    return; // For void methods, to exit the method.
  • Program 1 (Using return in a method):
    public class ReturnExample
    {
        public static void main(String[] args)
        {
            System.out.println("Result: " + addNumbers(5, 3)); // Calling method
        }
    
        public static int addNumbers(int a, int b)
        {
            int sum = a + b;
            return sum; // Return the sum to the caller
        }
    }
    Output:
    Result: 8
  • Program 2 (Using return in a void method):
    public class ReturnVoidExample
    {
        public static void main(String[] args)
        {
            checkAge(16); // Testing with an age less than 18
            // checkAge(20); // Testing with an age greater than or equal to 18
            System.out.println("Voting Ended.");
        }
    
        public static void checkAge(int age)
        {
            if (age < 18)
            {
                return; // Exits the method early if age is less than 18
            }
            System.out.println("You can vote");
        }
    }
    Output:
    Voting Ended.