Goto Statement In Java

Goto Statement

In this article we will be discussing about Goto Statement in Java.

A goto statement in any  programming language provides an unconditional jump from the goto to a labeled statement in the same function.

Goto function

Goto Statement 

Goto Statement in Java isn’t supported by Java. It is reserved as a keyword in the Java Virtual Machine,In case if they want to add it in a later version.
Instead of Goto function, Java uses Label. Labels are used to change the flow of the program and jump to a specific instruction or label based on a condition.

Use of Break And Continue in Label:

Break and continue are used to break or to continue the program when the label function is called. These are the two reserved keywords in Java Virtual Machine.

Goto Statement in java

Use case of break statement with label:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Label name for the break statement
        Lakshit:

            for (int i = 1; i <= 5; i++) {
                for (int j = 1; j <= 5; j++) {
                    if (j == 2)

                        // break statement for the label
                        break Lakshit;
                    System.out.println("PrepInsta " + j);
                }
            }   
    }
}
Output:

PrepInsta 1

Use case of Continue statement with label:

Run
import java.util.*;

public class Main {
    public static void main(String[] args){
        // Label name for the continue statement
        Lakshit:

            for (int i = 1; i <= 5; i++) {
                for (int j = 1; j <= 5; j++) {
                    if (j == 2)

                        // Continue Statement
                        continue Lakshit;
                    System.out.println("PrepInsta " + j);
                }
            }   
    }
}
Output:

PrepInsta 1
PrepInsta 1
PrepInsta 1
PrepInsta 1
PrepInsta 1
PrepInsta 1

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

2 comments on “Goto Statement In Java”