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

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

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 String. Apart from the decimal separator,...

Java Tutorial #2 – Operators in Java

Table of Contents Introduction 1. Assignment Operator 2. Arithmetic Operators 3. Compound Operators 4. Increment & Decrement Operators 5. Relational Operators 6. Logical Operators Introduction Operators are an essential...

Â

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 #4 – Control Statements

Introduction Control statements are used to change the flow of execution based on changes to certain variables in the code. One of the types of...

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

Java Tutorial #5 – Loop Statements

Iteration statements are used to repeat a particular block of code until a certain condition is true. In this article, we will be taking...

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

OTHER TUTORIALS

Common Errors with QTP/UFT – Part 1

As the name suggests, this article focuses on the common problems faced during or after QTP/UFT installation. We shall...

XML Framework Project

- Advertisement -spot_img