Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I am new to java and have been trying to implement goto. The compiler is throwing an error saying

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token "goto", assert expected
    restart cannot be resolved to a variable
at Chapter_3.Lab03_Chapter3.Factorial.main(Factorial.java:28)

Can someone tell me how to properly implement the goto statement or anything that acts like it? 

1 Answer

0 votes
by (13.1k points)

In Java, you do not have a goto statement. Instead, you have to use the break and continue like this:

public class Main

{

public static void main(String[] args) {

FIRST:

for(int i=0;i<5;i++){

    for(int j=0;j<5;j++){

        if(i==2 && j==3)

        break FIRST;

        else if(i==1 && j==2)

        continue FIRST;

        System.out.println("i: "+i+" j: "+j);

    }

  

}

}

}

Interested to learn Java? check out the Java course from Intellipaat 

Related questions

+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 17, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Apr 14, 2021 in Java by dante07 (13.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...