Does JAVA support goto statement

Tuesday, January 03, 2017 Unknown 0 Comments


Java does not support goto statement, but it supports label. label is useful in case of nested loops.

for example

public class LabelTest{
   public static void main{
   outer:
     for(int i=0;i<=5;i++){

         for(int j=0;j<=5;j++){
                System.out.println(j);
           if(j==2){
              break outer;
                         }
                }

         }

}

}


compile and run the above program
javac LabelTest.java
java LabelTest

output
0
1

            

0 comments: