循环结构

循环结构

-   while循环
-   do...while循环
-   for循环
-   for循环

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

while循环

-   while是最基本的循环,它的结构为
public class demo1 {
    public static void main(String[] args) {
        while(布尔表达式){
//            循环内容
        }
    }

}
-   只要布尔表达式为true,循环就会一直执行下去
-   我们大多数情况是会让循环停止下来,我们需要一个让表达式失效的方式来结束循环
-   少部分情况需要循环一直执行,比如服务器的请求响应监听等
-   循环条件一直为true就会造成无限循环(死循环),我们正常的业务编程中应该尽量避免死循环,会影响程序性能或者造成
-  程序卡死崩溃
-   思考:计算1+2+。。。100 = ?
public class demo1 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while(i <= 100){
//            循环内容
            sum = sum + i;
            i = i +1;
        }

        System.out.println(sum);
    }
}

do...while循环

-   对于while语句而言,如果不满足条件,则不能进入循环,但有时候我们需要即使不满足条件也至少执行一次
-   do...while循环和while循环相似,不同的是,do...while循环至少执行一次
do{}while(布尔表达式)
-   while和do...while的区别
    1 while先判断后执行,dowhile是先执行后判断
    2 dowhile总是保证循环体会被至少执行一次!这是他们的主要差别
public class demo1 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do{
            //  循环内容
            sum = sum + i;
            i = i +1;
        }while (i <= 100);

        System.out.println(sum);
    }
}
public class demo1 {
    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);
        }while (a < 0);
    }
}

For循环

-   虽然所有循环结构都可以用while或者do...while表示,但Java提供了另一种语法--for循环,
-    使循环结构更简单
-   for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构
-   for循环执行的次数是在执行前就确定,语法格式如下:
for(初始化;布尔表达式;更新){
//    代码语句
        }
-   练习1:计算0到100之间的奇数和偶数的和
-   练习2:用while或者for循环输出1-1000之间能被5整的数,并且每行输出3个
    练习3:打印九九乘法表
public class demo1 {
    public static void main(String[] args) {
//        int i = 0;
//        int j = 0;
//        int k = 0;
//
//        while (k <= 100) {
//            if (k % 2 > 0) {
//                i = k + i;
//            } else {
//                j = k + j;
//            }
//            k++;
//        }

//        System.out.println("奇数和为:" + i + "偶数和为:" + j);

/*
* 关于for循环有一下几点说明:
* 最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句
* 然后检测布尔值表达式的值,如果为true,循环体被执行,如果为false,循环终止,开始执行循环体后面的语句。
* 执行一次循环后,更新循环控制变量(迭代因子控制变量的增减)
* 再次检测布尔值表达式,循环执行上面的过程
* */
        for (int i = 0; i <= 1000; i++) {
            if(i % 5 == 0){
                System.out.print(i + "\t");
            }

            if (i % 15 == 0){
                System.out.println();
            }

//            println   输出完会换行
//            print 输出完不会换行
        }
        System.out.println("For循环结束");
    }
}
public class demo1 {
    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("");
        }

    }
}

增强for循环

-  Java5引入了一种主要用于数组和集合的增强型for循环
-  Java增强for循环语法格式如下:
for(声明语句:表达式){}
-   声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配,其作用域限定在循环语句块,其值与此时数组元素
-   相等
-   表达式:表达式是要访问的数组名,或者是返回值为数组的方法
public class demo1 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30};//定义数组
        
//        遍历数组的元素
        for(int x : numbers){
            System.out.println(x);
        }
    }
}
posted @ 2023-07-25 15:53  小安排  阅读(7)  评论(0编辑  收藏  举报