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

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

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

Concatenate Strings Using StringBuffer.append()

In addition to String, Java has the java.lang.StringBuffer and java.lang.StringBuilder classes. These can also be used to concatenate Strings. The advantage of these...

Java Tutorial #3 – Java Arrays

Table of Contents One Dimensional Array Declaring Array Allocting Memory to Array Accessing Array Elements Initializing Array Length of Array Multi Dimensional Array Creating 3...

Â

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

Convert String to Date Using java.util.Calendar

The java.util.Calendar class also encapsulates a date. The Calendar class has some more features than the java.util.Date class. It provides the ability to extract...

Java DecimalFormat Class

When you need to format decimal numbers, such as taking three or two decimal places for a number, showing only the integer part of...

Convert List to Array Using Stream with Param

There is an overloaded version of the Stream.toArray method which can be used to return a result array that is of the same data...

Concatenate Strings Using StringBuilder.append()

In addition to String, Java has the java.lang.StringBuffer and java.lang.StringBuilder classes. These can also be used to concatenate Strings. The advantage of these...

OTHER TUTORIALS

How To Do API Testing with JMeter

Introduction Application Programming Interface is a very popular term among developers. It is simply a request provider that responds to...

JMeter Framework Project

- Advertisement -spot_img