Java Tutorial #6 – Jump Statements

Learn Java Jump Statements - Break, Continue & Return

Table of Contents

Introduction

The break statement is used to stop further execution. It can be used either in a loop or within a switch statement.

Break Statement

The break statement is used to stop further execution. It can be used either in a loop or within a switch statement.

Break in a loop

When the break statement is used within a loop, it terminates the loop. So, the rest of the body of the loop is skipped and control is transferred to the first statement outside the loop even if the condition part of the loop is true. The break statement can be used with all Java loops like while, do-while, for and for-each. The following code demonstrates a break statement within a for loop:

int[] input = { 10, 20, 30,40,50 };

for (int num : input) {
if(num > 25) {
		break;
	}
System.out.println("Num:"+num);
}
System.out.println("Outside loop");

This code declares an array and uses a for-each loop to iterate through the array. The loop executes normally for the values 10 and 20. When the value 30 is encountered, the if condition that checks if the number is greater than 25 becomes true and so the break gets executed. This terminates the loop and control is transferred outside the loop. So, this code produces the following output:

Num:10
Num:20
Outside loop

Break in Switch statement

The break statement can also be used within a case statement in a switch block. In such a scenario, it terminates the case statement when a match is found, so no further case statements are checked. The following code demonstrates a break within a switch statement:

String dept = "IT";
switch(dept) {
case "HR": 
	System.out.println("Human Resources");
	break;
case "IT": 
	System.out.println("Information Technology");
	break;
case "Admin": 
	System.out.println("Administration");
	break;
default: 
	System.out.println("Invalid dept");
	break;
}
System.out.println("Outside switch");

This code uses a switch block.  It compares the value in the dept variable with each case value. A break is specified in each case statement. The variable dept matches the second case statement and so the value Information Technology gets printed to the console. Since a break statement is present, no further case statements are checked and control transfers outside the switch block. So, this code produces the following output:

Information Technology
Outside switch

If the break statement is not specified, then each case statement is checked even after a successful match.

Continue Statement

The continue statement is used to stop the current iteration of a loop. As soon as the continue statement is encountered the rest of the body of the loop is skipped and control transfers to the top of the loop. The condition is checked again and the loop is continued as long as the condition is true. The following code demonstrates the continue statement:

int[] input = { 10, 20, 30,40,50 };

for (int num : input) {
if(num == 30) {
continue;
}
System.out.println("Num:"+num);
}
System.out.println("Outside loop");

This code declares an array and uses a for-each loop to iterate through the array. The loop executes normally for the values 10 and 20. When the value 30 is encountered, the if condition that checks if the number is equal to 30 becomes true and so the continue gets executed. This skips the Sysout statement for the value 30 and control is transferred to the top of the loop. The loop then executes for the other values in the array. So, this code produces the following output:

Num:10
Num:20
Num:40
Num:50
Outside loop

Return Statement

The return statement is used to return control from a method to an invoking method. It can optionally return a value. As soon as return is encountered, control is transferred to the invoking method.

The following code demonstrates the return statement:

public void greet() {
String greeting = sayHello("Earth");
	System.out.println(greeting);
}
	
public String sayHello(String name) {
return "Hello "+name;
}

This code declares a method called greet() which invokes the sayHello() method. The sayHello() method uses the return statement to return a String value. As soon as return is encountered, control is transferred to the invoking method, in this case, the greet() method.

So, this code produces the following output: Hello Earth

Conclusion

Java jump statements help to transfer control to a different part of the code. The break statement terminates a loop and transfers control outside the loop. It can also be used to terminate a case statement in a switch block. The continue statement skips the current iteration of the loop, transfers control to the top of the loop and continues the next iteration. The return statement is used to return control from a method to an invoking method.

Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

JAVA TUTORIALS

Recent Posts

RELATED POSTS

Concatenate Strings Using String.concat()

The String class has a method called concat. It accepts as parameter a String value and concatenates it with the current String. The following...

How To Convert String To Date in Java

Introduction There are often scenarios in programming, where you will need to convert a date in String format to an actual Date object. For example,...

How To Sort List in Java

Introduction A List is an interface in the Java collection framework. It can be used to store objects. Programmers often encounter scenarios where they need...

How To Concatenate Strings in Java

Introduction In Java, a String is a sequence of characters. There are often programming situations where you will need to concatenate Strings. There are several...

Â

CHECKOUT 'HOW-TOs'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string...

MORE ON CODEX

MORE IN THIS CATEGORY

Remove Duplicates from List Using Stream

Java 8 has added the Stream API that helps to easily perform bulk operations on Collections. A new method called stream() method has been...

Format Decimal Numbers Using Format Symbols

You can customize which symbols are used as decimal separator, grouping separator, currency seperator etc. using a DecimalFormatSymbols instance together with java.text.DecimalFormat class. The...

Convert String to Date Using SimpleDateFormat Class

Sometimes, you may need to convert a String to a java.util.Date object. For this, you need to use the SimpleDateFormat class. You need to...

Early Bound Vs Late Bound (Binding)

What is Binding Before we dive into 'Early' and 'Late' stuff, let us discover, what the 'Binding' itself means? In simple terms, the 'Binding' refers to...

OTHER TUTORIALS

Format Decimal Numbers using Strings Within Pattern

As mentioned in earlier posts, the java.text.DecimalFormat class is used to format decimal numbers via predefined patterns specified as...
- Advertisement -spot_img