Java流程控制

Java流程控制,第1张

循环结构

本文md文件

while 循环
public class Day3 {
    public static void main(String[] args) {
        int a = 0;
        while (a<10){//表达式为true时一直执行,也就是所谓的【死循环】
            a=++a;
            System.out.println(a);
        }
    }
}
do…while 循环
public class Day3 {
    public static void main(String[] args) {
        int x = 0;
        int y = 0;
        do {//do...while先执行一遍,后进入循环
            x = x + 1;
            y = x + y;
        }while (x<100);
        System.out.println(y);
    }
}
for 循环
for(初始化;布尔表达式;更新){
    代码语句
}
===================================
public class Day3 {
    public static void main(String[] args) {
        int x = 0;//初始化条件
        while (x<=100){//条件判断
            System.out.println(x);//循环体
            x+=2;//迭代
        }
        //     初始化  ;循环体; 迭代
        for (int i = 1;i<=100;i++){
            //先执行初始化,可为空
            //检测布尔表达式的值,如果是true,执行循环体
            //执行循环更新变量再次检测布尔表达式
            System.out.println(i);
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/871904.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存