循环结构(Cyclic structure)

循环结构

  • while循环

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

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

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

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

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

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

    思考:计算1+2+3…..+100=?
    package com.tang.struct;public class WhileDemo01 {    public static void main(String[] args) {        //输出1–100        int i = 0;        while (i<100) {           ;            i++;            System.out.println(i);       }   }}//while (ture){ 为死循环 尽量避免!=================================================================   package com.tang.struct;public class whileDemo02 {    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);       }   }}

  • while是最基本的循环,它的结构为:
  • 只要布尔表达式为true,循环就会一直执行下去。
  • 我们大多数情况是会让循环停止下来的,我们需要一个表达式失效的方式来结束循环
  • 少部分情况需要循环一直执行,比如服务器的请求响应监听等。
  • 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!
  • 思考:计算1+2+3…..+100=?
    package com.tang.struct;public class WhileDemo01 {    public static void main(String[] args) {        //输出1–100        int i = 0;        while (i<100) {           ;            i++;            System.out.println(i);       }   }}//while (ture){ 为死循环 尽量避免!=================================================================   package com.tang.struct;public class whileDemo02 {    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);       }   }}
package com.tang.struct;public class WhileDemo01 {    public static void main(String[] args) {        //输出1--100        int i = 0;        while (i<100) {            ;            i++;            System.out.println(i);        }    }}//while (ture){  为死循环 尽量避免!=================================================================   package com.tang.struct;public class whileDemo02 {    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);        }    }}
  • do…while循环

    对于while语句而言,如果不满足条件,则不能进入循环,但有时候我们需要即使不满足条件,也至少执行一次。

    do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。
    do{
    //代码语句
    }while(布尔表达式);

    while和do…while的区别:

    while先判断后执行。do…while是先执行后判断!

    do…while总是保证循环体会被至少执行一次!这是最大的区别。

    package com.tang.struct;//示例public class DoWhileDemo03 {    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.tang.struct;//区别public class DoWhileDemo04 {    public static void main(String[] args) {        int a =0;        while (a<0){            a++;       }        //上面的没有执行        System.out.println("===========================================");        do {            System.out.println(a);//输出0            a++;       }while (a<0);   }}

  • 对于while语句而言,如果不满足条件,则不能进入循环,但有时候我们需要即使不满足条件,也至少执行一次。
  • do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。
    do{
    //代码语句
    }while(布尔表达式);
  • while和do…while的区别:

    while先判断后执行。do…while是先执行后判断!

    do…while总是保证循环体会被至少执行一次!这是最大的区别。

  • while先判断后执行。do…while是先执行后判断!
  • do…while总是保证循环体会被至少执行一次!这是最大的区别。
package com.tang.struct;//示例public class DoWhileDemo03 {    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.tang.struct;//区别public class DoWhileDemo04 {    public static void main(String[] args) {        int a =0;        while (a<0){            a++;        }        //上面的没有执行        System.out.println("===========================================");        do {            System.out.println(a);//输出0            a++;        }while (a<0);    }}
  • for循环

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

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

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

    for(初始化;布尔表达式;更新){
    //代码语句
    }
    package com.tang.struct;public class ForDemo01 {    public static void main(String[] args) {        int a = 1;//初始化条件        while (a<=100){//条件判断            System.out.println(a);//循环体            a+=2;//迭代 (a=a+2)       }        System.out.println("while循环结束!");        //初始化//条件判断//迭代        for (int i =1;i<=100;i++){    //快捷键 : 100.for            System.out.println(i);       }        System.out.println("for循环结束!");        /*        关于for循环有一下几点说明:        最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。        然后,检测布尔表达式的值。如果为true,循环被执行。如果为false。循环终止,开始执行循环后面的语句。        再次检测布尔表达式。循环执行上面的过程。         */        //死循环        for (; ; ){       }   }} 练习1:计算0-100之间的奇数和偶数的和 package com.tang.struct;public class ForDemo02 {    public static void main(String[] args) {        //练习1:计算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);   }} 练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个 package com.tang.struct;public class ForDemo03 {    public static void main(String[] args) {        //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个        for (int i = 0; i <= 1000; i++) {            if (i%5==0){                System.out.print(i+"\t");           }            if (i%(5*3)==0){  //每行                System.out.println();                //System.out.println("\n")           }       }        //println 输出完会换行        //println 输出完不会换行   }} 练习3:打印九九乘法表 package com.tang.struct;/*1×1=11×2=2 2×2=41×3=3 2×3=6 3×3=91×4=4 2×4=8 3×4=12 4×4=161×5=5 2×5=10 3×5=15 4×5=20 5×5=251×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=361×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=491×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=641×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 */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(j+"*"+i+"="+(j*i)+"\t");           }            System.out.println();       }   }}

  • 虽然所有循环结构都可以用while或者do…while表示,但Java提供了另一种语句—for循环,使一些循环结构变得更加简单。
  • for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
  • for循环执行的次数是在执行前就确定的。语法格式如下:
package com.tang.struct;public class ForDemo01 {    public static void main(String[] args) {        int a = 1;//初始化条件        while (a<=100){//条件判断            System.out.println(a);//循环体            a+=2;//迭代  (a=a+2)        }        System.out.println("while循环结束!");        //初始化//条件判断//迭代        for (int i =1;i<=100;i++){    //快捷键  :  100.for            System.out.println(i);        }        System.out.println("for循环结束!");        /*        关于for循环有一下几点说明:        最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。        然后,检测布尔表达式的值。如果为true,循环被执行。如果为false。循环终止,开始执行循环后面的语句。        再次检测布尔表达式。循环执行上面的过程。         */        //死循环        for (; ; ){        }    }}
  • 练习1:计算0-100之间的奇数和偶数的和
package com.tang.struct;public class ForDemo02 {    public static void main(String[] args) {        //练习1:计算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);    }}
  • 练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
package com.tang.struct;public class ForDemo03 {    public static void main(String[] args) {        //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个        for (int i = 0; i <= 1000; i++) {            if (i%5==0){                System.out.print(i+"\t");            }            if (i%(5*3)==0){  //每行                System.out.println();                //System.out.println("\n")            }        }        //println 输出完会换行        //println 输出完不会换行    }}
  • 练习3:打印九九乘法表
package com.tang.struct;/*1×1=11×2=2 2×2=41×3=3 2×3=6 3×3=91×4=4 2×4=8 3×4=12  4×4=161×5=5 2×5=10 3×5=15 4×5=20 5×5=251×6=6 2×6=12 3×6=18 4×6=24 5×6=30  6×6=361×7=7 2×7=14 3×7=21 4×7=28 5×7=35  6×7=42  7×7=491×8=8 2×8=16 3×8=24 4×8=32 5×8=40  6×8=48  7×8=56 8×8=641×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 */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(j+"*"+i+"="+(j*i)+"\t");            }            System.out.println();        }    }}
  • 在Java5中引入了一种主要用于数组的增强for循环。

    这里我们只是先见一面做个了解,之后数组我们重点使用

    Java5引用了一种主要用于数组或集合的增强型for循环

    Java增强for循环语法格式如下:

    for(声明语句:表达式)
    {
    //代码句子
    }

    声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句快,其值与此时数组元素的值相等。

    表达式:表达式是要访问的数组名,或者是返回的值为数组方法。

    package com.tang.struct;public class ForDemo05 {    public static void main(String[] args) {        int[] numbers ={10,20,30,40,50};//定义了一个数组        //遍历数组的元素        for (int x:numbers){//增强for            System.out.println(x);            System.out.println(“===================”);            for (int i = 0; i < 5; i++) {  //基础for                System.out.println(numbers[i]);           }       }   }}  

  • 这里我们只是先见一面做个了解,之后数组我们重点使用
  • Java5引用了一种主要用于数组或集合的增强型for循环
  • Java增强for循环语法格式如下:
  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句快,其值与此时数组元素的值相等。
  • 表达式:表达式是要访问的数组名,或者是返回的值为数组方法。
package com.tang.struct;public class ForDemo05 {    public static void main(String[] args) {        int[] numbers ={10,20,30,40,50};//定义了一个数组        //遍历数组的元素        for (int x:numbers){//增强for            System.out.println(x);            System.out.println("===================");            for (int i = 0; i < 5; i++) {  //基础for                System.out.println(numbers[i]);            }        }    }}
————————

Cyclic structure

  • while Loop
    While is the most basic loop. Its structure is:
    While (Boolean expression){
    //Loop content
    }
    As long as the Boolean expression is true, the loop will continue to execute.
    In most cases, we will stop the loop. We need an expression invalidation method to end the loop
    In a few cases, the loop needs to be executed all the time, such as the server’s request response listening.
    If the loop condition is true all the time, it will cause infinite loop [dead loop]. We should try to avoid dead loop in normal business programming. Will affect the program performance or cause the program to jam and crash!
    Thinking: calculate 1+2+3+ 100=?
    package com. tang. struct; Public class whiledemo01 {public static void main (string[] args) {/ / output 1–100 int i = 0; while (i<100) {; i++; system.out.println (I); }}}//while (true) {try to avoid loops! ==================================================================== package com.tang.struct; public class whiledemo02 {public static void main (string[] args) {/ / calculate 1+2+3+....+100=? Int i =0; int sum =0; while (i<=100) {            sum =sum +i;            i++;            System.out.println(sum);       }   }}
  • While is the most basic loop. Its structure is:
  • As long as the Boolean expression is true, the loop will continue to execute.
  • In most cases, we will stop the loop. We need an expression invalidation method to end the loop
  • In a few cases, the loop needs to be executed all the time, such as the server’s request response listening.
  • If the loop condition is true all the time, it will cause infinite loop [dead loop]. We should try to avoid dead loop in normal business programming. Will affect the program performance or cause the program to jam and crash!
  • 思考:计算1+2+3…..+100=?
    package com.tang.struct;public class WhileDemo01 {    public static void main(String[] args) {        //输出1–100        int i = 0;        while (i<100) {           ;            i++;            System.out.println(i);       }   }}//while (ture){ 为死循环 尽量避免!=================================================================   package com.tang.struct;public class whileDemo02 {    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);       }   }}
package com.tang.struct;public class WhileDemo01 {    public static void main(String[] args) {        //输出1--100        int i = 0;        while (i<100) {            ;            i++;            System.out.println(i);        }    }}//while (ture){  为死循环 尽量避免!=================================================================   package com.tang.struct;public class whileDemo02 {    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);        }    }}
  • do… while Loop
    For the while statement, if the condition is not satisfied, it cannot enter the loop, but sometimes we need to execute it at least once even if the condition is not satisfied.
    do… The while loop is similar to the while loop, except that do The while loop executes at least once.
    do{
    //Code statement
    }While (Boolean expression);
    While and do The difference between while:
    While judge first and then execute. do… While is to execute first and then judge!
    do… While always ensures that the loop is executed at least once! This is the biggest difference.
    package com. tang. struct;// Example public class dowiledemo03 {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. tang. struct;// Difference between public class dowhiledemo04 {public static void main (string[] args) {int a =0; while (a<0) {a++; } / / system out. println("==========================================="); Do {system.out.println (a); / / output 0 a++;       }while (a<0);   }}
  • For the while statement, if the condition is not satisfied, it cannot enter the loop, but sometimes we need to execute it at least once even if the condition is not satisfied.
  • do… The while loop is similar to the while loop, except that do The while loop executes at least once.
    do{
    //Code statement
    }While (Boolean expression);
  • While and do The difference between while:
    While judge first and then execute. do… While is to execute first and then judge!
    do… While always ensures that the loop is executed at least once! This is the biggest difference.
  • While judge first and then execute. do… While is to execute first and then judge!
  • do… While always ensures that the loop is executed at least once! This is the biggest difference.
package com.tang.struct;//示例public class DoWhileDemo03 {    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.tang.struct;//区别public class DoWhileDemo04 {    public static void main(String[] args) {        int a =0;        while (a<0){            a++;        }        //上面的没有执行        System.out.println("===========================================");        do {            System.out.println(a);//输出0            a++;        }while (a<0);    }}
  • For loop
    Although all loop structures can be used with while or do While, but Java provides another statement — for loop, which makes some loop structures simpler.
    The for loop statement is a general structure that supports iteration. It is the most effective and flexible loop structure.
    The number of times the for loop is executed is determined before execution. The syntax format is as follows:
    For (initialization; Boolean expression; update){
    //Code statement
    }
    package com. tang. struct; Public class fordemo01 {public static void main (string[] args) {int a = 1; / / initialize the condition while (a<=100) {/ / judge the condition system.out.println (a); / / loop body a+=2; / / iterate (a=a+2)} system out. Println ("while loop ends!"); / / initialization / / condition determination / / iteration for (int i =1; i<=100; i++) {/ / shortcut: 100.for system.out.println (I);       }        System. out. Println ("end of for loop!"); / * there are some notes about the for loop: the initialization step is performed first. A type can be declared, but one or more loop control variables can be initialized, or it can be an empty statement. Then, detect the value of the Boolean expression. If true, the loop is executed. If false. The loop terminates and starts executing the statements following the loop. Detect the Boolean expression again. Loop through the above procedure. * / / / loop for (;;) {       }   }} Exercise 1: calculate the sum of odd and even numbers between 0-100 package com. tang. struct; Public class fordemo02 {public static void main (string[] args) {/ / exercise 1: calculate the sum of odd and even numbers between 0--100 / / odd and int oddsum = 0; / / even and int eventsum = 0; for (int i = 0; I < = 100; i++) {if (i%2! =0) {/ / odd oddsum+=i; //oddsum = oddsum + I}else {/ / even eventsum+=i;           }                   }        System. out. Println ("odd sum" +oddsum);        System. out. Println ("even sum" +eventum);   }} Exercise 2: use the while or for loop to output numbers that can be divided by 5 from 1 to 1000, and output 3 numbers per line package com. tang. struct; Public class fordemo03 {public static void main (string[] args) {/ / exercise 2: use the while or for loop to output a number between 1 and 1000 that can be divided by 5, and output 3 for (int i = 0; I < = 1000; i++) {if (i%5==0) {system.out.print (i+ "\t") per line; } if (i% (5*3) ==0) {/ / system.out.println(); //system.out.println ("\n")}} / / println will wrap after output / / println will not wrap after output}} Exercise 3: print the 99 multiplication table package com. tang. struct;/* one × 1=11 × 2=2 2 × 2=41 × 3=3 2 × 3=6 3 × 3=91 × 4=4 2 × 4=8 3 × 4=12 4 × 4=161 × 5=5 2 × 5=10 3 × 5=15 4 × 5=20 5 × 5=251 × 6=6 2 × 6=12 3 × 6=18 4 × 6=24 5 × 6=30 6 × 6=361 × 7=7 2 × 7=14 3 × 7=21 4 × 7=28 5 × 7=35 6 × 7=42 7 × 7=491 × 8=8 2 × 8=16 3 × 8=24 4 × 8=32 5 × 8=40 6 × 8=48 7 × 8=56 8 × 8=641 × 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 */public class fordemo04 {public static void main (string[] args) {/ / 1. print the first column first, //2. hold the fixed 1 in a loop //3. remove the duplicate items, i<=j //4. adjust the style 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();       }   }}
  • Although all loop structures can be used with while or do While, but Java provides another statement — for loop, which makes some loop structures simpler.
  • The for loop statement is a general structure that supports iteration. It is the most effective and flexible loop structure.
  • The number of times the for loop is executed is determined before execution. The syntax format is as follows:
package com.tang.struct;public class ForDemo01 {    public static void main(String[] args) {        int a = 1;//初始化条件        while (a<=100){//条件判断            System.out.println(a);//循环体            a+=2;//迭代  (a=a+2)        }        System.out.println("while循环结束!");        //初始化//条件判断//迭代        for (int i =1;i<=100;i++){    //快捷键  :  100.for            System.out.println(i);        }        System.out.println("for循环结束!");        /*        关于for循环有一下几点说明:        最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。        然后,检测布尔表达式的值。如果为true,循环被执行。如果为false。循环终止,开始执行循环后面的语句。        再次检测布尔表达式。循环执行上面的过程。         */        //死循环        for (; ; ){        }    }}
  • Exercise 1: calculate the sum of odd and even numbers between 0-100
package com.tang.struct;public class ForDemo02 {    public static void main(String[] args) {        //练习1:计算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);    }}
  • Exercise 2: use the while or for loop to output numbers that can be divided by 5 from 1 to 1000, and output 3 numbers per line
package com.tang.struct;public class ForDemo03 {    public static void main(String[] args) {        //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个        for (int i = 0; i <= 1000; i++) {            if (i%5==0){                System.out.print(i+"\t");            }            if (i%(5*3)==0){  //每行                System.out.println();                //System.out.println("\n")            }        }        //println 输出完会换行        //println 输出完不会换行    }}
  • Exercise 3: print the 99 multiplication table
package com.tang.struct;/*1×1=11×2=2 2×2=41×3=3 2×3=6 3×3=91×4=4 2×4=8 3×4=12  4×4=161×5=5 2×5=10 3×5=15 4×5=20 5×5=251×6=6 2×6=12 3×6=18 4×6=24 5×6=30  6×6=361×7=7 2×7=14 3×7=21 4×7=28 5×7=35  6×7=42  7×7=491×8=8 2×8=16 3×8=24 4×8=32 5×8=40  6×8=48  7×8=56 8×8=641×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 */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(j+"*"+i+"="+(j*i)+"\t");            }            System.out.println();        }    }}
  • An enhanced for loop, mainly for arrays, has been introduced in java5.
    Here we are just looking at it first, and then we will focus on using arrays
    Java5 refers to an enhanced for loop that is primarily used for arrays or collections
    The Java enhanced for loop syntax format is as follows:
    For (declaration statement: expression)
    {
    //Code sentence
    }
    Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the loop statement, and its value is equal to the value of the array element at this time.
    Expression: the expression is the name of the array to be accessed, or the returned value is an array method.
    package com. tang. struct; public class ForDemo05 {    public static void main(String[] args) {        int[] numbers ={10,20,30,40,50};// Defines an array / / traverses the elements of the array for (int x:numbers) {/ / enhanced for system.out.println (x); system.out.println (“==================”); for (int i = 0; I < 5; i++) {/ / basic for system.out.println (numbers[i]);           }       }   }}
  • Here we are just looking at it first, and then we will focus on using arrays
  • Java5 refers to an enhanced for loop that is primarily used for arrays or collections
  • The Java enhanced for loop syntax format is as follows:
  • Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the loop statement, and its value is equal to the value of the array element at this time.
  • Expression: the expression is the name of the array to be accessed, or the returned value is an array method.
package com.tang.struct;public class ForDemo05 {    public static void main(String[] args) {        int[] numbers ={10,20,30,40,50};//定义了一个数组        //遍历数组的元素        for (int x:numbers){//增强for            System.out.println(x);            System.out.println("===================");            for (int i = 0; i < 5; i++) {  //基础for                System.out.println(numbers[i]);            }        }    }}