Java Selection Statements: Jump Statements
Jump Statements
Java supports three jump statement: break, continue and return. These three statements transfer control to other part of the program.
break
We have seen and used break before (see the switch statement above). Typically break is used for terminating a sequence in a switch statements, but it can also exit a loop or perform a form of goto statement.
We haven’t covered loops yet, so just know that a for loop in Java will continue to execute the same code block over and over until a certain condition is no longer true. Typically this condition will be when a variable is long longer less that a preset value.
Using break to exit a loop
To use break to exit a loop you need to insert it into your code at an appropriate location, typically this will be where the loop is no longer required to continue. For example if your code was searching a known number of records but you only wanted one particular record you could insert an if statement that would execute a break when that record was found since at the moment looping through the remaining records was no longer necessary. Here’s an example of a break used within a loop:
int x; System.out.println("Loop will repeat for x < 10"); for ( x = 0; x < 10; x++) { //Terminate the loop when x equals 5 if (x == 5) break; //Output the current value of x System.out.println("x: " + x); } System.out.println("Loop exited when x equals " + x);
The output of this code is shown below:
run: Loop will repeat for x < 10 x: 0 x: 1 x: 2 x: 3 x: 4 Loop exited when x equals 5 BUILD SUCCESSFUL (total time: 0 seconds)
You can see that the by using break we forced an immediate termination of a loop, even though there was another 5 loops remaining had the for loop been allowed to continue. Later, you will see that, like if statements, loops can be nested and if a break is used within a nested loop it will only break out of the innermost loop.
Using break as a form of goto statement
If you are familiar with the C language, Pearl or some procedural languages you would have used the goto statement. This provides a one-way transfer of control to another line of code, essentially jumping to or going to a new line within the program. Java does not support the goto statement but we can use a break statement in tandem with labels to perform the same type of event as a goto statement.
In Java, a label identifies a block of code. You can call a label anything you want, avoiding the usual pitfalls like Java keywords of course. The general syntax is:
labelName:
{
statement1;
statement2;
statementN;
}
Once a label exists within your code, a break statement can be used to jump from the position of the break to the end of the label associated with the break, effectively performing a goto event. The general syntax is:
break labelName;
Let’s see this in action:
label: { System.out.println("One"); System.out.println("Two"); break label; System.out.println("Three"); } System.out.println("Four");
The label can be called anything (avoiding Java keywords of course), for simplicity we have called this one label. The label’s code block contains three output statements what will output the strings “One”, “Two”, and “Three” in that order, and there is one more output statement outside the label’s code block that will output the string “Four”.
Without a break we would expect to see all four strings output in the order that we see them in the code, but we do have a break and it is positioned after the string “Two” – so Java should shift its focus from the break position to the first statement is sees AFTER the label’s code block:
run: One Two Four BUILD SUCCESSFUL (total time: 2 seconds)
As expected, this is what we see in the output. The string “Three” was bypassed by the break as Java executed the first statement after the label’s code block.
Let’s look at a more complex example. Below we have three nested labels; firstLabel, secondLabel and thirdLabel. At start and end of each label we have inserted an output statement indicating which label Java is current starting or ending.
Without any breaks we would expect to see an output like “Starting firstLabel”, “Starting secondLabel”, “Starting thirdLabel”, “Ending thirdLabel”, “Ending secondLabel”, and finally “Ending firstLabel”.
By positioning a break secondLabel statement at line 14, which is within the thirdLabel code block, we should immediately shift Java’s focus to the statement after the secondLabel (which should be with the firstLabel):
int x = 5; firstLabel: { System.out.println("Starting firstLabel"); secondLabel: { System.out.println("Starting secondLabel"); thirdLabel: { System.out.println("Starting ThirdLabel"); if (x == 5) break secondLabel; System.out.println("Ending thirdLabel"); } //End of thirdLabel System.out.println("Ending secondLabel"); } //End of secondLabel System.out.println("Ending firstLabel"); } //End of firstLabel
The output should be:
run: Starting firstLabel Starting secondLabel Starting ThirdLabel Ending firstLabel. BUILD SUCCESSFUL (total time: 0 seconds)
As expected, Java executes all code up to line 14, the break statement, and then jumps to line 20, the first statement after the code block associated with secondLabel.
continue
The continue statement is typically used to force an early iteration of a loop. A loop will execute all statements within its code block over and over as long a condition is true, but with a continue statement you can exit a loop at any point within its code block and continue with the next iteration of the loop. You could consider this another type of goto statement where Java will goto the end of the loop before starting the next iteration of the loop.
Let’s look at an example of using a continue statement. We have a simple loop that will output a sequence of integers from 0 to 5. To do this we will use a for loop that will loop as long as the integer value is less than 6:
for (int x = 0; x < 6; x++) { // Prints all numbers System.out.println(x); }
As expected the output is a sequence of numbers from 0 to 5:
run: 0 1 2 3 4 5 BUILD SUCCESSFUL (total time: 0 seconds)
Now, let’s introduce a continue statement that will exit the loop if the number is an odd number, or if the modulo is not equal to zero:
for (int x = 0; x < 6; x++) { // Stop processing here // and continue with the // next loop if the number // is odd if (x%2 != 0){ continue; } // Prints only even numbers System.out.println(x); }
Now, the loop is interrupted if x modulo 2 is not equal to zero (x%2 != 0), or if 2 divides into x with a remainder meaning x must be an odd number. So, for every odd number the loop iteration will exit before outputting the value of x and proceed with the next iteration. This will result in the output of only even numbers:
run: 0 2 4 BUILD SUCCESSFUL (total time: 0 seconds)
As expected, only even numbers are output.