Jump Statements in JAVA - Types of Statements in JAVA (With Examples)

Jump Statements in JAVA - Types of Statements in JAVA (With Examples)

11 Sep 2024
Beginner
18.6K Views
11 min read
Learn with an interactive course and practical hands-on labs

Java Online Course Free with Certificate (2024)

Jump Statements in JAVA: An Overview

Jump statements in Java are control flow statements that let you change the order in which programs execute. These statements provide flexibility and control over program logic to the programmer. If you are looking to enhance your Java programming skills and gain a comprehensive understanding of jump statements, consider enrolling in Full Stack Java Developer Course. In this Java tutorial, we'll cover these statements and how they're used with Java.

What are Jump Statements in Java?

In Java, a jump statement is used for transferring the control of the program from one particular point to another point of the program, allowing for changes in the flow of execution.

This particular statement works by jumping from one specific code to another one, thus exhibiting polymorphism in Java, to the flow of the execution of the program. "Jump Statements" are also called "Branching Statements in Java" as they evaluate different branches to enhance the flow of the execution.

Read More - Advanced Java Interview Questions

Types of Jump Statements in Java

There are three major variations of the jumping statements in JAVA. Those are "Break statement", "Continue statement" and "Return statement". These will be discussed further in this article. Not only that we’ll give you a clear idea about Java constructor in our next article.

Types of Jump Statements in Java

Read More: Looping Statements in Java

1. Break statement in Java

  • A break statement in Java assists in terminating 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, 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".

Break statement in Java

Syntax

break;

Read More - Java Developer Salary

Example: Use Break to Exit a Loop

It is generally used for getting exits from any existing loop. We'll see the demonstration of the break statement in our Java Compiler.
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

Example: Use Break as a form of goto

In Java "Goto" statement is generally used as a "Break label" statement for executing a particular label block.
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

Example: Use Break in a Switch case

It assists in bypassing all other statements and jumping to the "switch statement".
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 

Read More: Switch Statement in Java

2. 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.

Continue statement in Java

Syntax

Continue;

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

3. 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".

Syntax

return;

Example

Let's see the working of the return statement in the Java Playground.
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

It's important to understand jump statements in Java, as they are essential tools in a programmer's toolkit. With the break, continue, and return statements 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, which is a valuable skill to acquire during Full Stack Java Course.

FAQs

In Java, the Goto statement is not a valid jump statement. Java does not support the Goto statement as it can lead to unstructured and hard-to-maintain code. Instead, Java provides other control flow statements like break, continue, and return to manage the flow of execution in a more structured manner.

The System.exit(int status) method is used to terminate the program in Java. It's not a traditional jump statement, but it can be employed to immediately exit the program with a specified status code. The System.exit(0) indicates a successful termination, while a non-zero value typically signifies an error.

The goto statement is not part of Java's syntax, but historically, it has been used in other programming languages. On the other hand, the break statement is a valid Java jump statement used to terminate the loop or switch statement prematurely.

Jump statements in programming, such as break, continue, and return in Java, are used to alter the normal flow of control in a program. Jump statements provide a way to handle various scenarios in the control flow of a program, allowing for greater flexibility and control over the execution process. 
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this