Is there a goto statement in Java?

前端 未结 23 1956
醉酒成梦
醉酒成梦 2020-11-22 04:50

I\'m confused about this. Most of us have been told that there isn\'t any goto statement in Java.

But I found that it is one of the keywords in Java. Where can it be

23条回答
  •  别跟我提以往
    2020-11-22 05:32

    Because it's not supported and why would you want a goto keyword that did nothing or a variable named goto?

    Although you can use break label; and continue label; statements to effectively do what goto does. But I wouldn't recommend it.

    public static void main(String [] args) {
    
         boolean t = true;
    
         first: {
            second: {
               third: {
                   System.out.println("Before the break");
    
                   if (t) {
                      break second;
                   }
    
                   System.out.println("Not executed");
               }
    
               System.out.println("Not executed - end of second block");
            }
    
            System.out.println("End of third block");
         }
    }
    

提交回复
热议问题