测试 break 的代码如下:

public class TestBreak {

  public static void main(String[] args) {

    for (int i = 0; i < 5; i++) {

      if (i == 2)

         break;

      System.out.println(i);

    }

  }

}

输出结果为:

0

1

测试 continue 的代码如下:

public class TestContinue {

  public static void main(String[] args) {

    for (int i = 0; i < 5; i++) {

      if (i == 2)

        continue;

      System.out.println(i);

    }

  }

}

输出结果为:

0

1

3

4

【结论】break用于终止整个循环,而continue用于终止某一次循环。

相关文章:

  • 2021-11-21
  • 2021-06-08
猜你喜欢
  • 2022-01-09
  • 2021-12-15
  • 2021-12-28
  • 2021-04-29
相关资源
相似解决方案