48_三种循环的区别
1 /* 2 三种循环的区别 3 */ 4 public class OperatorDemo{ 5 public static void main(String[] args){ 6 /* 7 //for 循环 8 for(int i = 3;i <3;i++){ 9 System.out.println("我爱Java"); 10 } 11 System.out.println("------"); 12 13 //while 循环 14 int j = 3; 15 while(j<3){ 16 System.out.println("我爱Java"); 17 } 18 19 //do...while 循环实现 20 int K = 3; 21 do { 22 System.out.println("我爱Java"); 23 K++; 24 }while(K<3); 25 */ 26 /* 27 //for 循环 28 for(int i = 3;i <3;i++){ 29 System.out.println("我爱Java"); 30 } 31 //System.out.println(i); 32 System.out.println("------"); 33 34 //while 循环 35 int j = 3; 36 while(j<3){ 37 System.out.println("我爱Java"); 38 } 39 System.out.println(j); 40 System.out.println("------"); 41 */ 42 43 /* 44 //死循环 45 for(;;){ 46 System.out.println("for"); 47 } 48 49 50 while(true){ 51 System.out.println("while"); 52 } 53 */ 54 do { 55 System.out.println("do...while"); 56 57 }while(true); 58 59 } 60 }
三种循环的区别
三种循环的区别
1.for循环和while循环先判断条件是否成立,然后决定是否执行循环体(先判断后执行)
2.do...while循环先执行一次循环体,然后判断条件是否成立,是否继续执行循环体(先执行后判断)
for 和 while 的区别
1.条件控制语句所控制的自增变量,因为归属for循环的语法结构中,在for循环结束后,就不能再次被访问到了;
2.条件控制语句所控制的自增变量,对于while循环来说不归属于其语法结构中,在while循环结束后,该变量还可以继续使用
死循环
for(;;){}
while(true){}
do{}while(true);
while的死循环格式是最常用的
命令提示窗口中ctrl+c可以结束死循环