The Command.. break; in Java what if.?

后端 未结 8 500
走了就别回头了
走了就别回头了 2021-02-03 17:50

What if we have an if statement inside a for loop, would it stop the loop or the if condition...

Example:

for (int i = 0; i < array.length; i++) {
           


        
8条回答
  •  时光说笑
    2021-02-03 18:05

    The selected answer is almost right. if break statement be mixed by label then it can be used in if statement without needing to be in a loop. The following code is completely valid, compiles and runs.

    public class Test {
        public static void main(String[] args) {
            int i=0;
            label:if(i>2){
                break label;
            }               
        }
    }
    

    However if we remove the label, it fails to compile.

提交回复
热议问题