Is there a goto statement in Java?

前端 未结 23 1955
醉酒成梦
醉酒成梦 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:56

    An example of how to use "continue" labels in Java is:

    public class Label {
        public static void main(String[] args) {
            int temp = 0;
            out: // label
            for (int i = 0; i < 3; ++i) {
                System.out.println("I am here");
                for (int j = 0; j < 20; ++j) {
                    if(temp==0) {
                        System.out.println("j: " + j);
                        if (j == 1) {
                            temp = j;
                            continue out; // goto label "out"
                        }
                    }
                }
            }
            System.out.println("temp = " + temp);
        }
    }
    

    Results:

    I am here // i=0
    j: 0
    j: 1
    I am here // i=1
    I am here // i=2
    temp = 1
    

提交回复
热议问题