循环结构

本文深入探讨了Java中的四种基本循环结构:while、do-while、for以及增强for循环。通过实例展示了它们的使用方式、特点和区别,强调了循环控制的重要性,包括如何避免死循环以及在实际编程中如何选择合适的循环结构。同时,提供了多个编程练习,如计算序列和、输出特定条件的数字等,以加深理解。
摘要由CSDN通过智能技术生成

while循环

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

while(布尔表达式){

​ //循环内容

}

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

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

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

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

package com.kuang.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.kuang.struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while (true){
            //等待客户端连接
            //定时检查
        }
    }
}
package com.kuang.struct;

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);
    }
}

DoWhile循环

do{

​ //代码语句

}while(布尔表达式);

While和do-While的区别:

◆while先判断后执行。dowhile是先执行后判断!

◆Do…while总是保证循环体会被至少执行一次! 这是他们的主要差别。

package com.kuang.struct;

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);
    }
}
package com.kuang.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);
    }
}

For循环

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

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

​ for(初始化; 布尔表达式; 更新){

​ //代码语句

​ }

package com.kuang.struct;

public class ForDemo01 {
    public static void main(String[] args) {
          int a=1;//初始化条件

        while (a<=100){//条件判断
            System.out.println(a);//循环体
            a+=2;//迭代
        }
        System.out.println("while循环结束!");

        //初始值//条件判断//迭代
        for (int i=1;i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束!");
    }
}
练习
package com.kuang.struct;

public class ForDemo02 {
    public static void main(String[] args) {
        //练习:计算0到100之间的奇数和偶数的和

        int oddSum=0;
        int eveSum=0;

        for(int i=0;i<100;i++){
            if(i%2!=0){//奇数
                oddSum+=i;
            }else {//偶数
                eveSum+=i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+eveSum);
    }
}
package com.kuang.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

        for (int i=1;i<=1000;i++){
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%15==0){
                System.out.println();
                //System.out.println("\n");
            }
        }
        //println 输出完全换行
        //print  输出完全不会换行
    }
}
package com.kuang.struct;

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+"="+(j*i)+"\t");
        }
            System.out.println();
        }
    }
}
增强for循环
package com.kuang.struct;

public class ForDemo05 {
    public static void main(String[] args) {
         int[] numbers={10,20,30,40,50};//定义了一个数组

        for (int i=0;i<5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("=========================");
        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值