循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复 执行这个循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循环,否则循环将一直执行下去, 形成死循环。

循环语句—for

for循环格式

  1. for(初始表达式;布尔表达式;步进表达式) {
  2. 循环体
  3. }

image.png

public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println("hello" + i);
        }
        }
    }

循环语句—while

while表达式

初始化表达式
while(布尔表达式) {
    循环体
  步进表达式
}

image.png

public class Test {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            System.out.println("hello" + i);
            i++;
        }
        }
    }

循环语句—do…while

do…while表达式

初始化表达式
do {
循环体
步进表达式
} while(布尔表达式);

image.png

public class Test {
    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.println("hello" + i);
            i++;
        } while (i < 10);
        }
    }

跳出循环

break

public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if(i==3) {
                break;
            }
            System.out.println("hello" + i);
        }
    }
}

continue

public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if(i==3) {
                continue;
            }
            System.out.println("hello" + i);
        }
    }
}