Site icon Automation Dojos

Java Tutorial #6 – Jump Statements

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.

Exit mobile version