java. cyclic structure

java loop structure

Order structure of the program statements can only be performed once. If you want to perform the same operation many times, we need to use cyclic structure.

There are three main java loop structure:

while loop
do ... while loop
for loop

In java5 introduced for enhancing a major array of for loop.

1.while cycle

while the most basic cycle, its structure is:
while (Boolean (true / false) expression) {
loop content
}
as long as the Boolean expression is true the loop will have been implemented.


//来看看实例吧:
public class Test {
    public static void main(String args[]) {
        int x = 10;
        while (x < 20) {
            System.out.print("value of x :" + x);
            x++;
            System.out.print("\n");
        }
    }
}

//以上实例编译运行结构如下:

value of x : 10
value of x : 11
...

value of x : 19

2.do ... while loop

For a while statement, if the conditions are not satisfied, they can not enter the circulation. But sometimes we need even if conditions are not satisfied, but also to perform at least once.
do ... while loop while loop and the same, except that,

do ... while loop will execute at least once.

do {
code statement
} while (Boolean expressions);

Note: Boolean expression after the loop body, the block of statements boolean expression prior to the detection has been performed. If the Boolean expression evaluates to true, the statement block
executes until the Boolean expression evaluates to false.

public class Test {
    public static void main(Staing args[]) {
        int x = 10;
        do {
            System.out.print("value of x :" + x);
            x++;
            System.out.print("\n");
        } while (x < 20);
    }
}

//以上实例编译运行结果如下:

value of x : 10
...
value of x :19

3.for cycle

While all loop structure can be used while or do ... while said, but java provides another statement (for recycling), the number of circulating structure easier.

The number of cycles performed for before executing it is determined
Syntax is as follows:


  for (  1初始化;  2布尔表达式; 4更新){
            代码语句
    }

//关于for循环有以下几点说明:
//1,最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
//2,然后,检测布尔表达式的值。如果是true,循环体被执行,如果是false,循环体终止,开始执行循环后面的语句。
//3,执行一次循环后,更新循环控制变量。
//4,再次检测布尔表达式。循环执行上面的过程。

public class Test{
    public static void main (Staing args[ ]){
    for(int x=10;x<20;x=x+1){
    System.out.print("value of x :"+x);
    System.out.print("\n");
            }
        }
}

Compiled results are as follows

value of x :10

value of x :19

4.java enhanced for loop

introducing a java5 primarily for enhanced rot loop array.
java enhanced for loop syntax is as follows:

   for(声明语句:表达式){
        代码句子
    }
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块
其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。


public class test {
    public static void main(String args[]) {
        int[] numbers = { 10, 20, 30, 40, 50 };
        for (int x : numbers) {
            System.out.print(x);
            System.out.print(",");
        }
        System.out.print("\n");
        String[] names = { "James", "Larry", "Tom", "Lacy" };
        for (String name : names) {
            System.out.print(name);
            System.out.print(",");
        }
    }
}

编译运行如下:

1020304050
James,Larry,Tom,Lacy,

break keyword

break主要用在循环语句或者switch语句中,用来跳出整个语句块。
break跳出最里面的循环,并且继续执行该循环下面的语句。

break examples:

public class Test {
   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ) {
         // x 等于 30 时跳出循环
         if( x == 30 ) {
            break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

以上实例编译运行结果如下:

10
20

continue keywords

continue the control loop is applicable to any structure. Role is to make the program immediately jump to the next loop iteration.
In the for loop, continue statement causes the program to jump immediately to update statements.
In while or do ... while loop, the program immediately jumps to the Boolean expression of judgment statement.

continue Example:

public class Test {
   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ) {
         if( x == 30 ) {
        continue;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

以上实例编译运行结果如下:

10
20
40
50
Published 11 original articles · won praise 0 · Views 73

Guess you like

Origin blog.csdn.net/qq_45797116/article/details/104068138