Java Basics (VI): cycling

cycle

Platitudes of a control process, and when we are using arrays and collections, the time element is often used to traverse the structure of the cycle, Java has a very flexible mechanism for three cycles:

image-20190827222828453

According to know whether the number of cycles can be divided into the while loop, do ... while loops and for loops, let alone take a look:

while loop

When we do not know the number of cycles can be used to operate while loop, while the following is pseudo code loop

定义初始变量  
while (控制条件) {
    循环体
}

image-20190827222337726

Code Example:

        //    定义控制循环变量
       int start = 0;
    //    循环条件
       while (start < 2) {
        //    循环体
        System.out.println("1");
        //  改变控制循环变量
        start++;
       }
   }

do ... while loop

And while loop, do ... while loop is equally applicable to the cycle time do not know the specific number, but not the same and while loops is that, if the control variable cycle initial cycle when not meet the conditions, then the loop once will not be executed, and do ... while loop body of the loop will execute at least once.

定义初始变量
do {
   循环体
} while (循环条件);

image-20190827223154870

Code Example:

        //    定义控制循环变量
       int start = 0;
    do {
        // 循环体
        System.out.println("1");
        // 控制循环变量
        start++;
        // 循环条件
    } while(start < 0);

for loop

Here we entered the main event, the largest circulation daily for use, due to the number of cycles for general circulation can be controlled accurately, it is generally the time when we need to manually control the number of cycles, we will use ordinary for loop

for(定义初始变量;判断条件;变量变化){
  循环体
}

Here are a flow chart and while similar, let's look at how to traverse an array:

int[] a = {1,2,3,4};
for (int i = 0; i < a.length; i++) {
  System.out.println(a[i]);
}

The same can also be used to traverse a set of common for loops:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
for (int i = 0; i < list.size(); i++) {
  System.out.println(list.get(i));
}

Here attentive students may have noticed, when we described here has been used for an ordinary cycle, since there are so ordinary for loop, there must be no ordinary for loop, let's look at two kinds of less-common for loop

Enhanced for loop

After JDK 5, there is a syntactic sugar --forEach cycle, also referred to as enhanced for loop, loop syntax

for(数据类型 定义元素名:循环列表) {
  循环体
}

foreach statement is a simplified version for a special statement, but can not completely replace the foreach statement for statement, however, any of the foreach statement can be rewritten as the for statement version. foreach is not a keyword, it is customary to call it "foreach" statement for this particular statement format.

On enhancing efficiency for general circulation and for loop

Array traversal: same Enhanced for loop and loop through general principle, the same efficiency.

Collection traversal: Enhanced for loop cycles through its essence is the iterator iterator traversal, and ordinary compared to loop through each scene has its own applicable, for example, are more suitable for general circulation for the List class (an array class) traversed by the subscript Find data, and the enhanced for loop is more suitable list set traverse structure.

In a larger amount of data in the case where, if it is used to enhance the collection efficiency of the cycle will be less than for normal use for loop.

Out of two keywords cycle

We use the process, if you encounter a situation need to interrupt the process, often use the following two keywords: breakand continue.

breakThe main loop or used in the switch statement to jump out of the entire block. break out of the loop the innermost layer, and the cycle continues following statement. Of course, we can also use tags to jump out of a given cycle.

read_data:
while(...) {
    for(...) {
        break read_data;    //这里就是直接跳出了while循环
    }
}

continueIt applies to any loop control structure of. Role is to make the program immediately jump to the next loop iteration. In the for loop continuestatement 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. Of course, continuethere are a form with a label, the header tag match cycle skip. Usage and break the same, there is no longer an example.

the public

Guess you like

Origin www.cnblogs.com/viyoung/p/12529440.html