常用循环

  • breack

  • contunue

for循环

//(1)只能在循环体内和switch语句体内使用break;

//(2)当break出现在循环体中的switch语句体内时,起作用只是跳出该switch语句体,并不能终止循环体的执行。若想强行终止循环体的执行,可以在循环体中,但并不在switch语句中设置break语句,满足某种条件则跳出本层循环体。

//2.continue

//continue语句的作用是跳过本次循环体中余下尚未执行的语句,立即进行下一次的循环条件判定,可以理解为仅结束本次循环。

//注意:continue语句并没有使整个循环终止。
public static void main(String[] args) {

    for (int i = 0; i <= 5; i++) {
        
        
        for (int j = 5; j >=i; j--) {
            System.out.print(" ");
        }
        for (int j = 1; j <=i; j++) {
            System.out.print("*");
        }
        for (int j = 1; j <i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }

}

while循环

public class While {
    public static void main(String[] args) {
        int i = 0;
        int s = 0;
        while (i < 100) {
            i++;
            s+=i;
        }
        System.out.println(s);
    }
}

do while

public class DoWhileDemo {
    public static void main(String[] args) {
        int i = 0;
        int s = 0;

        // 即使代码至少会在do里面执行一次
        do {
            i++;
            s += i;
        } while (i < 100);
        System.out.println(s);
    }
}
posted @ 2021-06-28 16:44  橙子yuan  阅读(81)  评论(0)    收藏  举报