Decision Making In Java Using Jump Statements | Part 2

Decision Making In Java Using Jump Statements | Part 2
Decision Making In Java Using Jump Statements | Part 2

Prerequisite:
Decision Making In Java Using If, Else-If And Switch Statements | Part 1

Introduction

Decision-making in Java supports three types of jump statements, namely, break, continue and return. These jump statements are also called Branching Statements in Java. They are used to transfer control to some other part of the program. 

This blog is a continuation of a two-blog series on Decision making in Java.

The first part discusses Decision making in Java using if, else-if, and switch statements.

Without further ado, let’s get started with Jump statements.

Break Statement

The break statement has three main functions in Java. These are mentioned below:

  • It terminates a case statement in a switch statement.
  • It can be used to get out of a loop.
  • It can be used as a “civilized” form of goto keyword.

 Flowchart for the break statement

Image Source: DataFlair

The first point is already discussed in the first part of this series. Let’s move on to understand the last two functions.

Using break to Exit a Loop: 

When a break statement is encountered in a loop, the loop is terminated, and control is passed to the next statement after the loop. Break terminates a loop immediately, bypassing any remaining code in the loop’s body.

Let’s see an example of it:

class BreakLoop 
{ 
public static void main(String args[]) 
{ 
for(int i=0; i<10; i++) 
{ 
if(i == 5) 
break; // terminate loop if i is 10 
System.out.println("i: " + i); 
} 
System.out.println("Loop complete."); 
} 
} 

Output:
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
Loop complete.

As we can see, although the for loop is designed to run from 0 to 10, the break statement made it terminate early, when i equals 5.

Using break as a Form of Goto

 Java does not support a goto statement because it allows random and unstructured branching. Also, goto-ridden code is generally tricky to understand. However, there are a few places where goto statements are needed. 

For all such cases, Java defines an expanded form of a break statement. By using this form, we can break out of one or more blocks of code at once. These blocks can be any block. Furthermore, this type of break statement uses a label to define exactly where the execution will resume.

The generalized form of the labeled break statement is shown here: 

break label; 

A label is a name that identifies a block of code. To put a label on a block, create a label at the start of it. A label is a valid Java identifier followed by a colon.

The program below demonstrates break as goto: It has three nested blocks, each with a unique label. The break statement will break the second block, skipping the two print statements.

public class Break 
{ 
public static void main(String args[]) 
{ 
boolean value = true; 
first: 
{ 
second: 
{ 
third: 
{ 

System.out.println("Before the break."); 
if(value) 
break second; // break out of second block
System.out.println("This won't execute"); 
} 
System.out.println("This won't execute"); 
} 
System.out.println("This is after the second block."); 
}
} 
}

Output:
Before the break.
This is after the second block.

Here are a few more points to keep in mind about ‘break’.

  • The break statement can be used with every loop in Java, including intentionally infinite loops.
  • Multiple break statements can be used inside a loop. 
  • The break that terminates a switch case affects only that switch statement and not any enclosing loops.
  • The most common use for a labeled break statement is to exit from nested loops.

Let’s move on to learn about the continue jump statement.

Continue statement

Sometimes it is needed to skip an iteration of a loop. We don’t want to process the code inside the loop for a particular iteration and continue running the loop for the next iteration. The continue keyword handles these types of conditions.

In while and do-while loops, a continue statement directly transfers the control to the conditional expression.

Flowchart for continue statement

The following is a program that uses continue to print two numbers on each line:

public class Continue 
{ 
public static void main(String args[]) 
{ 
for(int i=0; i<10; i++) 
{ 
System.out.print(i + " ");
if (i%2 == 0) 
continue; 
System.out.println(""); 
} 
} 
}

Output:
0 1
2 3
4 5
6 7
8 9

Labeled continue statement

Similar to the break statement, the continue statement can also specify a label to indicate which enclosing loop should be continued. Here is a program that uses a continuous statement to print a triangular multiplication table.

public class ContinueLabel 
{ 
public static void main(String args[]) 
{ 
outer: 
for (int i=0; i<10; i++) // outer loop
{ 
for(int j=0; j<10; j++) // inner loop
{ 
if(j > i) 
{ 
System.out.println(); 
continue outer; 
} 
System.out.print(" " + (i * j)); 
} 
} 
System.out.println(); 
} 
}

Output:
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81

Here, if condition (j>i) is satisfied, the continue statement terminates the inner loop and continues with the next iteration of the outer loop. For exceptional circumstances where early iteration is needed, the continued statement provides an organized way to do it.

Return statement

The return is the final control statement. The return statement in Java is used to return from a method explicitly. It causes program control to transfer back to the calling method. 

As a result, the return statement immediately terminates the method in which it is executed. The following example illustrates this point. Here, the return statement causes the execution to return to the Java run-time system.

Note: The Java run-time system calls the main method.

class Return 
{ 
public static void main(String args[]) 
{ 
boolean value = true;
System.out.println("Before the return."); 
if(value) 
return; // return to caller 
System.out.println("This won't execute!!"); 
} 
}

Output:
Before the return.

The final print statement is not executed because as soon as the return is executed,

control passes back to the calling function.

Note: In addition to the jump statements covered here, Java allows to change the program’s flow of execution via exception handling as well. Exception handling is a structured way of catching and handling run-time errors in the program. It uses the keywords try, catchy, finally, throw, and throws.

To learn more about each keyword in detail, check out our blogs on Exception Handling in Java using try-catch and finally and Decoding throw and throws in Exception Handling.

Frequently Asked Questions

What are jump statements?

Jump statements cause an unconditional jump to another statement in the program. They are used to interrupt switch statements and loops.

What are the two main jump statements in Java?

Break and continue are the two main jump statements in Java. The break is used to terminate the entire loop in the program, and the continue terminates one iteration of the loop.

Is there a goto statement in Java?

Java does not support goto statements, but it is a reserved keyword in Java. The variation of break and continue statements can be utilised as goto.

Which statement is used to skip an iteration in a loop?

The continue statement is used to skip an iteration inside a loop.

Why do we need jump statements?

Jump statements can be used to modify the working of conditional and iterative statements in Java. We can use jump statements to exit a loop, initiate the next iteration of a loop, or move program control to a specific position in our code.

Key Takeaways

Decision-making is an integral part of every programming language. This blog, in particular, discusses the jump statements such as continue, break, return, and their variations. 

So, we’ve reached the end of the two blog series on Decision making in Java. If you haven’t visited the first part yet, visit the blog Decision making in Java using if, else-if and switch statements| Part 1 and complete this series.

To get a clear understanding of this series, you can refer to this blog: Loops in Java.

Happy Learning!

By Vaishnavi Pandey