使用break语句只能跳出一层循环,但是在实际开发中,有时会碰到跳出到最外层循环或者指定位置,这时单纯使用break语句就不能很好达到预期效果.

JAVA提供了一种带标签的break语句,用于跳出多重嵌套的循环语句.

public static void main(String[] args) {
        /*
        * 测试带标签的break语句*/
        int i;
        int j;
        Label_out:
        for (i=0;i<=10;i++){
            for (j=0;j<10;j++){
                if (j>=5){
                    break Label_out;
                }
                System.out.println(j);  //0 1 2 3 4
            }
        }

    }

注意 Label_out 只能放在需要跳出的循环上面,  例如,让在int i 上面就会报错.

相关文章:

  • 2021-06-20
  • 2021-12-18
  • 2021-06-15
  • 2021-06-28
  • 2021-08-25
  • 2021-11-09
  • 2021-09-07
  • 2022-03-05
猜你喜欢
  • 2021-04-28
  • 2021-03-31
  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案