break 和 continue

 1 package com.ibeve.demo;
 2 
 3 public class OtherDemo {
 4 
 5     public static void main(String[] args) {
 6 
 7         /**
 8          * break 作用于循环内部,用于跳出循环,即循环语句结束了, 只跳出当前所在的内循环
 9          * 
10          * @param args
11          */
12         for (int x = 0; x < 3; x++) {
13             for (int y = 0; y < 4; y++) {
14                 System.out.println("x=" + x);
15                 break;
16             }
17 
18         }
19         // 标号只能用于循环上,给循环起名字的一种方式
20         w: for (int x = 0; x < 3; x++) {
21             q: for (int y = 0; y < 4; y++) {
22                 System.out.println("x=" + x);
23                 break w;
24             }
25 
26         }
27 
28         /**
29          * continue:只能作用于循环结构。继续循环,特点:结束本次循环,继续下一次循环
30          * 
31          */
32         for (int x = 1; x < 10; x++) {
33             if (x % 2 == 1)
34                 continue;
35             System.out.println("x=" + x);
36 
37         }
38 
39         w: for (int x = 1; x < 10; x++) {
40             if (x % 2 == 1)
41                 System.out.println("x=" + x);
42             continue w;
43 
44         }
45         /**
46          * 记住:
47          * 1.break 和 continue 语句作用的范围。
48          * 2.break 和 continue 单独存在时,下面可以有任何语句,因为都执行不到。
49          * break; 在 switch 或 loop 外部中断
50          * continue; 在 loop 外部中断 
51          */
52         
53     
54         
55     }
56 }
原文地址:https://www.cnblogs.com/believeus/p/8952785.html