Day08Java流程控制---循环结构

循环结构

  • while循环
  • do…while循环
  • for 循环
  • 在Java5中引入了一种主要用于数组的增强型for循环

while循环

  1. while是最基本的循环,他的结构为:
while(布尔表达式){
    //循环内容
}
  1. 只要布尔表达式为true,循环就会一直执行下去
  2. 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环
  3. 少部分情况需要循环一直执行,比如服务器的请求响应监听等。
  4. 循环条件一直为true就会造成无线循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!
  5. 思考:计算1+2+3+……+100=?
  • 循环:
package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 9:52
 */
public class WhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);

        }
    }
}
//输出结果就是1-100每个数都输出出来
  • 死循环
package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 9:55
 */
public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while (true){
            //等待客户端连接:QQ接收消息
            //定时检查
            //……
        }
    }
}

  • 计算1+2+3+……+100=?
package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 10:01
 */
public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+3+……+100=?
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum = sum + i;
            i++;
        }
        System.out.println(sum);

    }
}
//输出结果5050。

do…while 循环

  • 对于while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件也至少执行一次。
  • do…while 循环和while 循环相似,不同的是,do…while 循环至少会执行一次。

语法:

do{
    //代码语句
}while(布尔表达式)
  • while和do…while的区别:
    1. while先判断后执行,do…while是先执行后判断
    2. do…while总是保证循环体会被至少执行一次!这是最主要的差别。

do…while循环例子:

package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 13:19
 */
public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum = sum +i;
            i++;

        }while (i<=100);
        System.out.println(sum);
    }

}
//输出结果5050。

while和do…while的区别:

package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 13:23
 */
public class DoWhileDemo02 {
    public static void main(String[] args) {

        int a = 0;
        int sum =0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("==============");
        do {
            System.out.println(a);
            a++;
        }while (a<0);


    }
}
/**
输出结果:
==============
0
*/

For循环

  • 虽然所有循环结构都可以用while或者do…while表示,但Java提供了另一种语句------for循环,使一些循环结构变得更加简单。

  • for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。

  • For循环执行的次数是在执行前就确定的。语法格式如下:

    for(初始化值;布尔表达式;更新){
        //代码语句
    }
    
    1. 练习:计算0-100之间的奇数和偶数的和
    2. 练习:用while或for循环输出1-100之间能被5整除的数, 并且每行输出3个
    3. 练习:打印九九乘法表

for循环例子

package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 13:42
 */
public class ForDemo01 {
    public static void main(String[] args) {
        int i = 0;//初始化条件
        while (i<=100){//条件判断
            System.out.println(i);//循环体
            i++;//迭代,用以终止循环

        }
        System.out.println("while循环结束");
        System.out.println("=============");
        //初始化值;条件判断;更新
        for (int a = 0;a<=100;a++){
            System.out.println(a);
        }
        System.out.println("for循环结束");
    }
    /**
     * 关于for循环有一下几个点说明;
     * 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个变量,也可以是空语句。
     * 然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
     * 再次检测布尔表达式,循环执行上面的过程。
     */
}

练习1:计算0-100之间的奇数和偶数的和

package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 15:43
 */
public class ForDemo02 {
    public static void main(String[] args) {
        //计算0-100之间的奇数和偶数的和
        int oddsum = 0;
        int evensum = 0;
        for (int i = 0; i < 100; i++) {
            if (i%2!=0){//奇数
                oddsum+=i;//oddsum = oddsum + i;

            }else{//偶数
                evensum+=i;
            }
        }
        System.out.println("奇数和:"+oddsum);
        System.out.println("偶数和:"+evensum);
    }
}
/**
输出结果
奇数和:2500
偶数和2450
*/

练习2:用while或for循环输出1-100之间能被5整除的数, 并且每行输出3个

package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 16:24
 */
public class ForDemo03 {
    public static void main(String[] args) {
        //用while或for循环输出1-100之间能被5整除的数, 并且每行输出3个
        for (int i = 0; i < 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%15==0){//换行用的
                System.out.println();
                //System.out.print("\n");
            }
        }
    }
}
/**
输出结果
0	
5	10	15	
20	25	30	
………………
980	985	990	
995	
*/

练习3:打印九九乘法表

package com.FlyFish.Struct;

/**
 * @auther FlyFish
 * @company wu
 * @create 2021-03-11 21:07
 */
public class ForDemo04 {
    public static void main(String[] args) {
        /**
         * 1.先打印第一列
         * 2.把固定的1再用一个循环包起来
         * 3.去掉重复项  i<=j
         * 4.调整样式
         */
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i+"*"+j+"="+(i*j)+"\t");
        }
            System.out.println();
        }
    }
}
//输出结果
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值