Jumping statements are control statements that transfer execution control from one point to another point in the program. There are three Jump statements that are provided in the Java programming language:
Java does not include a goto statement for altering program flow. While goto is a reserved keyword in Java, it is not implemented and cannot be used in code. This design choice was made to promote structured programming practices and avoid the creation of difficult-to-maintain "spaghetti code" often associated with goto in other languages like C or C++.
Use Break as a form of goto
Java does not have a goto statement because it produces an unstructured way to alter the flow of program execution. Java illustrates an extended form of the break statement. This form of break works with the label. The label is the name of a label that identifies a statement or a block of code.
Syntax:
break label;
When this form of break executes, control jumps out of the labeled statement or block.
Here is an example:
import java.io.*;
class GFG {
public static void main(String[] args)
{
for (int i = 0; i < 3; i++)
{
one : { // label one
two : { // label two
three : { // label three
System.out.println("i=" + i);
if (i == 0)
break one; // break to label one
if (i == 1)
break two; // break to label two
if (i == 2)
break three; // break to label three
}
System.out.println("after label three");
}
System.out.println("after label two");
}
System.out.println("after label one");
}
}
}