Jump Statements in Java

Jump Statements in Java

4 mins read17.1K Views Comment
Updated on Jan 25, 2023 11:27 IST

In this article, we’ll cover the Jump Statements in Java, such as break, continue and return, along with examples and explanations.

2022_06_jump-statements.jpg

Contents

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
350 hours
Free
6 months
– / –
4 months
– / –
– / –
– / –
1 month
50 K
7 weeks
7.8 K
3 months
8.47 K
2 months
7 K
6 months
4 K
2 months

Introduction

As we know, the normal flow of execution of programming statements is sequential. However, we can alter the flow using several control statements in Java. Jump statements are one of the types of control statements in Java that directs the flow of execution in a program. 

Usually, there are 3 types of Control Statements in Java:

The below article covers Jump Statements in Java. Let’s Begin.

What are Jump Statements in Java?

Jump statements help amend the flow of execution by jumping to another piece of code in the program. That’s why it is also known as branching statements, as they create a different branch to the flow of execution.

Jump Statements in Java

Following are the Jump statements in Java:

types

Break Statement

The break statement is commonly used with the iteration statements such as for loop, while loop, and do-while loop. 

Break statements in Java generally used in 3 ways:

Let’s go through it one by one.

  1. Exiting a Loop

A break statement uses to exit an existing loop, especially an infinite 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);
}
}
}
}
Copy code

Explanation:

Here, the for loop is in an infinite state as there is no termination condition declared. Hence, it prints the number until it goes into the if clause, which has a condition if i equals 5, then break out of the for loop.

Note: The above break statement is called an unlabelled break. It is commonly used with switch and loops.

Output:

output1

Read: Implementing Array in Java

  1. As a form of Goto

Goto statements are commonly used in traditional programming languages like C. In Java, this construct transfers the control from one part of the program to another. 

Note: Java does not use goto statements as it generates a lot of unmaintainable codes. Instead, it uses break as a form of goto.

Syntax:

break label;

The above statement branch controls a block of statements named label. The statements under the label name within curly braces are said to be inside the 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");
}
}
}
Copy code

Explanation:

The above example uses 3 labels: label_1, label_2, and label_3. The label_3 has a for loop, which should have run 100 times. But, it didn’t, as we used the if clause with a break statement. Hence, the loop runs only 4 times, printing “Inside Label_3”. Consequently, the break statement jumps out of the label_3 block and prints further statements.

Output:

output2

Also Read: Difference between JDK, JRE, and JVM

  1. In a Switch Case

A break statement is used in switch case statements to bypass all the other cases and jumps out of 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!");
}
}
}
Copy code

Output:

output3

Also, Read: Constructors in Java Explained

Continue Statement

Unlike break, the continue statement in java can only work inside loops. It doesn’t come out of the loop like a break. Instead, it forces the next iteration of the loop to bypass all the statements falling under it. 

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);
}
}
}
Copy code

Explanation: 

Here, the continue statement for i<3 bypasses the below statement and goes to the next iteration.

Output:

Read: Java Operators Explained

Return Statement

Return statements are the jump statements used inside methods (or functions) only. Any code in the method which is written after the return statement might be treated as unreachable by the compiler.

Then return statement terminates the execution of the current method and passes the control to the calling method.

It can exist in 2 forms:

  1. Return with a Value:

Here, the return statement returns a value to the calling method. The value can be a primitive data type or reference type.

Syntax:

return value;

  1. Return without a Value:

It simply transfers the control back to the calling method without any value.

Syntax:

return;

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!");
}
}
Copy code

Output:

Also Read: 8 Most Important Data Structures Every Programmer Must Know

Conclusion

Hope the explanation of Jump statements in Java helped you understand it better. If you have any queries please share them on the link below. We’d appreciate your efforts. Keep Learning.

Read: Top 160 Java Interview Questions and Answers for 2022

______________

Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online.

FAQs

What is the break statement?

A break statement uses to exit an existing loop and a switch case. It gets the control out of the current loop.

What is the continue statement?

Continue statement forces the next iteration of the loop to bypass all the statements falling under it.

What is the return statement?

Then return statement terminates the execution of the current method and passes the control to the calling method.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio