Java Goto
Unlike other programming languages, Java does not have goto
. Instead, Java contains the keyword label
.
The keyword label
is to change a program’s flow and jump to another section of the program based on the specified condition.
As we know, two basic keywords, break
and continue
, are used to change the direction of a loop. If you are working with loops, the keyword continue
will skip a specific part of code inside your loop based on the condition and the keyword break
will take you to program out of the loop based on the specific condition.
The keyword label
is only useful before the nested loop statements. You can also use the specified label name by combining the break
and continue
keywords.
In this article, we are going to see how we can use the keyword label
in our Java program and also we are going to discuss the topic by using necessary examples and explanations to make the topic easier.
Java Keyword label
With the Keyword Break
In our example below, we will learn how to use label
in our Java program. The example is very simple.
The code will be as follows:
class JavaGoto {
public static void main(String args[]) {
end:
for (int i = 0; i < 9; i++) {
if (i == 5) {
break end;
}
System.out.println("Current number is: " + i);
}
}
}
In the example, we first created a class named JavaGoto
and a label named end:
. After that, we created a loop through the line for (int i=0;i<9;i++)
, and inside the loop, we created a condition to stop the loop when the value of i
equals 5.
When the value of i
equals 5, we will break the loop and print the output.
You will get the below output when you run the program.
Current number is: 0
Current number is: 1
Current number is: 2
Current number is: 3
Current number is: 4
Java Keyword label
With the Keyword continue
In this next example, we will look at another example where we combined label
with the keyword continue
. The code will look like this:
class JavaGoto {
public static void main(String args[]) {
cont:
for (int i = 0; i < 9; i++) {
if (i == 5) {
continue cont;
}
System.out.println("Current number is: " + i);
}
}
}
In the example above, we first created a class named JavaGoto
and a label name cont:
. After that, we created a loop through the line for (int i=0;i<9;i++)
, and inside the loop, we created a condition to skip the action for the loop when the value of i
equals 5.
When the value of i
equals 5, we will skip the current action for the loop and print the output.
Now when you run the above program, you will get an output like the one below.
Current number is: 0
Current number is: 1
Current number is: 2
Current number is: 3
Current number is: 4
Current number is: 6
Current number is: 7
Current number is: 8
Please note that the code examples shared here are in Java, and you must install Java on your environment if your system doesn’t contain Java.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn