for与while使用场景区别
for(i=0;i<3;i++)————循环3次 for(i=1;i<=3;i++)————循环3次for循环中的i只在for循环中有用,while循环中控制循环的变量在循环后还可以继续使用
do-while
特点:先执行后判断且一定会执行一次
死循环
public static void test1() { for (;;) { System.out.println("hello world"); } //while循环实现死循环 while (true) { System.out.println("hello world"); }循环嵌套
外部循环每循环一次,内部循环会全部执行完一轮
System.out.print("*") //不换行打印 System.out.println("*") //换行打印99乘法表
public static void print99() { for(int i=1;i<=9;i++) { for(j=1;j<=i;j++) { System.out.print(j+"x"+i+"="+(j*i)+"\t"); } System.out.println(); } }break、continue
//for循环break跳出当前循环 public static void test1() { for (int i = 1; i <= 5; i++) { if (i == 3) { break; } System.out.println("i=" + i); } } //3及3以后的数字均打印 //for循环continue跳出当前循环,继续下一次循环 public static void test2() { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } System.out.println("i=" + i); } } //只有3未打印,剩余数字均打印