Day07-循环结构

循环结构

一.while循环

while是最基本的循环,它的结构为:

while(布尔表达式){
    //循环内容
}

只要布尔表达式为true,循环就会一直进行下去

我们大多数情况是会让循环停止下来的,我们需要让一个表达式失效的方式来结束循环

少部分情况需要循环一直进行,比如服务器的请求响应监听等

循环条件一直为true就会造成无限循环【死循环】。我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!

例:

package com.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        //输出1-100
        int i=0;
        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}

例:

package com.struct;

public class WhlieDemo02 {
    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的区别:

  • while先判断后执行,do...while是先执行后判断
  • do...while总是保证循环体至少会被执行一次!这是他们的主要差别。

例:

package com.struct;

public class DoWhileDemo {
    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
    }
}

例:

package com.struct;

public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a=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(初始化;布尔表达式;更新){
    //代码语句
}

例:

package com.struct;

public class ForDemo01 {
    public static void main(String[] args) {
            //初始化值 条件判断 迭代
        for (int a=1;a<=100;a+=2) {
            //执行一次循环后,更新循环控制变量,再次检测布尔表达式,执行循环内容
            System.out.println(a);
        }
        System.out.println("for循环结束");
       //循环次数.fori(++)
       //循环次数.forr(--)
    }
}

例:

package com.struct;

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=oddSum+i;
            }else{
                evenSum=evenSum+i;
            }
        }
        System.out.println("奇数的和"+oddSum);//2500
        System.out.println("偶数的和"+evenSum);//2550
    }
}

加一个小知识:

  • println输出完会换行
  • print输出完不会换行
  • 还有个换行符\n不要忘了...

四.实例:打印九九乘法表

package com.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j + "*" + i + "=" + (j * i)+"\t");
            }
            System.out.println();
        }
    }
}
//输出结果
//1*1=1	
//2*1=2	2*2=4	
//3*1=3	3*2=6	3*3=9	
//4*1=4	4*2=8	4*3=12	4*4=16	
//5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
//6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
//7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
//8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
//9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	
posted @ 2022-03-21 21:06  LiLiLiYH  Views(30)  Comments(0)    收藏  举报