- public class BreakVsContinue {
- public static void main(String[] args) {
- int n;
- System.out.println ("starting loop:");
- for (n = 0 ; n < 7; ++n)
- {
- System.out.println ("in loop: " + n);
- if (n == 2) {
- continue;
- }
- System.out.println (" survived first guard");
- if (n == 4) {
- break;
- }
- System.out.println (" survived second guard");
- // continue at head of loop
- }
- // break out of loop
- System.out.println ("因为在第" + n + "次循环中遇到break,循环结束" );
- }
- }
复制代码 continue:本次循环结束,循环体内剩余的部分跳过,进入到下一次循环
break:遇到break,整个循环结束,后面的几次循环都不再执行。
|
|