break contiue
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,
不执行循环中的剩余语句。(break语句也在switch语句中使用)
continue 语句用在循环语句体重,用于终止某次循环过程,即跳过循环体中尚未执行的语句,
接着进行下一次是否执行循环的判定
1 package com.xl.struct; 2 3 public class BreakDemo { 4 public static void main(String[] args) { 5 int i = 0; 6 while (i<100){ 7 i++; 8 System.out.println(i); 9 if (i==30){ 10 break; 11 } 12 } 13 System.out.println("结束循环"); 14 } 15 }
1 package com.xl.struct; 2 3 public class ContinueDemo { 4 public static void main(String[] args) { 5 int i = 0; 6 while (i<100){ 7 i++; 8 if (i%10==0){ 9 System.out.println(); 10 continue;// 11 } 12 System.out.print(i); 13 } 14 } 15 16 }
1 package com.xl.struct; 2 3 public class LabelDemo { 4 public static void main(String[] args) { 5 //打印101-105之间的所有质数 6 //质数是指在大于1的自然数中,除了1和它本身以外不再有其他因素的自然数。 7 int count = 0 ; 8 outer:for (int i =101;i<150;i++){ 9 for (int j = 2; j<i/2;j++){ 10 if (i % j == 0){ 11 continue outer; 12 } 13 } 14 System.out.print(i+"\t"); 15 } 16 } 17 }