Java Jump Statements: break, continue, return

07 Feb 2023
Beginner
346 Views

Java Jump Statements: break, continue, return

Introduction

Are you ready to take your Java programming skills to the next level? The key to mastering coding is understanding how the pieces work together. That's why taking a look at jump statements, an essential part of every code language, can help give you an in-depth mastery of writing great software. In this article, we'll cover what these special commands are and how they're used with Java so that you can start putting them into practice and see results faster!

What are Jump statements in Java?

A jump statement is basically used for transferring the control of the program from one particular point to another point program. This particular statement works by jumping from one specific code to another one in order to amend the flow of the execution of the program."Jump Statements" is also called "Branching Statements" as it evaluates different branches for enhancing the flow of the execution.

Types of Jump Statements in Java

There are three major variations of the jump statement in JAVA. Those are "Break statement", "Continue statement" and "Return statement". These will be discussed further in this article.

Break statement in Java

  • A break statement in Java assists to terminate the execution of a loop in a particular program.
  • This statement transfers the control of the program to the next statement which is immediately after the current loop or switch.
  • A break statement is used in an existing statement of a loop, especially an infinite loop, in order to exit from it.
  • There are 3 types of Break Statements, those are "Exiting a Loop", "As a form of Goto", and "In a switch case".

Exiting a Loop
It is generally used for getting exits from any existing loop.
Example
class break_statement
{
    public static void main(String[] args)
{
     int i=1;
        for (;;i++) //infinite loop
        {
         if (i==5)
            {
             break;
            }
             else
            {
             System.out.println(i);
            }
        }
    }
}
Output
1
2
3
4
As a form of Goto
In Java "Goto" statement is generally used as a "Break label" statement for executing a particular label block.
Example
class break_statement_goto
{
 public static void main(String[] args)
 {
   label_1:
    {
     label_2:
      {
        label_3:
         {
           for(int i=0; i<100; i++)
             {
              System.out.println("Inside Label_3");
                if (i==3)
                break label_3;
             }
          }
            System.out.println("Inside Label_2");
        }
            System.out.println("Inside Label_1");
     }
  }
}
Output
Inside Label_3
In a switch case
It assists to bypass all other statements and jump to the "switch statement".
Example
class Switch_Case
{
    public static void main(String[] args)
    {
        char a = 'C';
        switch (a)
        {
            case 'A':
                System.out.println("Letter A");
                break; //break statement to come out of switch
            case 'C':
                System.out.println("Letter C");
                break;
            default:
                System.out.println("Default case: NO Letter Matched!");
        }
    }
}
Output
Letter C 

Continue statement in Java

  • The continue statement in Java never terminates the execution of any loops.
  • It can only work inside the "loop statement".
  • The primary job of the "Continue statement" is the iteration of that specific loop.
  • Continue statement assists the bypass of all the other statements by making them fall under it.
  • The nature of the Continue statement is to skip the current iteration and force for the next one.

Example
class continue_statement
{
  public static void main(String[] args)
    {
      for(int i=0; i<=10;i++)
        {
          if (i<3)
            {
              Continue; //continues and goes to next iteration
            }
          System.out.println(i);
        }
    }
}
Output
3
4
5
7
8
9
10

Return statement in Java

  • The return statement in Java is a type of jump statement which is primarily used only inside the functions or methods.
  • If in a method, any code is written after using the Return statement that can be treated as an unreachable statement by the compiler.
  • The purpose of using a Return statement is to terminate the current method of execution and transfer the control to the next "calling method".
  • There are two types of Return statements, which are "Return with a value" and "Return without a value".
Example
class continue_statement
{
  public static void main(String[] args)
    {
      int age = 12;
      System.out.println("Using Return");
      if (age<18)
      return; //terminates the method
      System.out.println("Will not get executed!");
    }
}
Output
Using Return 

Summary

In conclusion, it's important to understand Java jump statements as they are essential tools in a programmer's toolkit. With the break, continue, and return statements all at your disposal you can ensure that your code is efficient, optimized, and easily readable. As each of these statements has its unique applications, it's important to understand where and when to make use of them. With proper knowledge of these three basic commands, you'll be able to take your programming skills to the next level. So make sure you keep these statements top of mind when crafting our programs and web pages! Code like a pro with java jump statements.

Accept cookies & close this