1. public class DoWhileDemo6 {
    2. public static void main(String[] args) {
    3. // 目标:学会使用dowhile循环,并理解其执行流程
    4. int i = 0;
    5. do {
    6. System.out.println("HelloWorld"); // dowhile第一次先执行do语句,然后在执行循环语句
    7. i++; // 也是执行3次HelloWorld,第一次为0,直接执行,然后再加1,执行while循环判断
    8. } while (i<3);
    1. // for循环中,控制循环的变量只在循环中可以使用。While循环中,控制循环的变量在循环后还可以继续使用
    2. for (int j = 0; j < 3; j++) {
    3. System.out.println("HelloWorld");
    4. }
    5. System.out.println(j); // idea冒红了,不能在for循环内使用for循环中的变量
    1. System.out.println("------------------------------");
    2. int m = 0;
    3. while (m<3){
    4. System.out.print("Hello World" + "\t");
    5. m++;
    6. System.out.print(m + "\t"); // 每次循环都输出m,查看m值得变化
    7. }
    8. System.out.println(m + "\t");// while可以在外面输出变量,查看最终值
    9. // m为3,因为m=3时不执行循环
    10. }
    11. }