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

Remove Duplicates from List Using LinkedHashSet

Another implementation of the Set interface is LinkedHashSet. LinkedHashSet maintains the order of elements and helps to overcome the HashSet limitation. The following code...

Convert List to Array Using ToArray With Param

There is an overloaded version of the toArray method. This accepts an array as a parameter and returns a result array that is of the same...

Format Decimal Numbers Using Locale

If you want to create a DecimalFormat instance for a specific Locale, create a NumberFormat and cast it to a DecimalFormat. The java.text.DecimalFormat class is...

Format Decimal Numbers with Grouping Separator

The DecimalFormat class has a method called setGroupingSize() which sets how many digits of the integer part to group. Groups are separated by the...

Â

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

Java Tutorial #1 – Variables and Data Types

Table of Contents Introduction 1. Variables 2. Data Types Primitive Data Types Integer Data Types Decimal Data Types Character Data Types Boolean Data Types Reference...

Concatenate Strings Using String.join()

Java 8 has added a static join method to the String class. This concatenates Strings using a delimiter. Performance wise, it is similar to...

Concatenate Strings Using Plus Operator

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

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

OTHER TUTORIALS

QA Challenges in Mobile App Testing

Mobile application testing is always challenging. Smaller footprints, numerous types of devices and changing technologies are some of the...
- Advertisement -spot_img