Advanced Java from entry to the road (four)

Previous article, we introduced the Java operators and expressions, in this chapter we look at Java looping constructs.

Loop process is a computer programming language, some code is repeatedly executed, is a group of the same or similar statement is performed regularly repeatability.

Cycle of elements:

  - initializing the loop variable

  - cycling conditions (variable in cycle basis)

  - loop variable change (toward the end of the cycle of change)

There are three main Java looping constructs:

  • while  loop
  • do ... while  loop
  • for  loop

while loop

while the most basic cycle, its structure is:

the while (Boolean expressions) {
     // cycle Content 
}

As long as the Boolean expression is true, the loop will have been implemented.

1 public class HelloWorld {
2     public static void main(String[] args) {
3         int i = 0;
4         while (i < 5) {
5             System.out.print(i); // 0 1 2 3 4
6             i++;
7         }
8     }
9 }

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 and while loop is similar, the difference is, do ... while loop will execute at least once.

do { 
  // code statement } the while (Boolean expression);

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.

1 public class HelloWorld {
2     public static void main(String[] args) {
3         int i = 0;
4         do {
5             System.out.print(i); // 0 1 2 3 4
6             i++;
7         } while (i < 5);
8     }
9 }

for loop

While all loop structure can be used while or do ... while said, but Java provides another statement - for circulation, so some of the loop structures easier.

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

for (initialization; Boolean expression; update) {
     // code statement 
}

About the for loop has the following description:

  • The first to perform the initialization procedure. May declare a type, but may initialize one or more of the loop control variable, the statement may be empty.
  • Then, the Boolean expressions to detect. If true, the loop body is executed. If false, the loop terminates, started behind the loop statement.
  • After executing a loop, update loop control variable.
  • Boolean expression is detected again. Loop executes the above process.
1 public class HelloWorld {
2     public static void main(String[] args) {
3         for (int i = 0; i < 5; i++) {
4             System.out.print(i); // 0 1 2 3 4
5         }
6     }
7 }

Java enhanced for loop

Java5 introduced enhanced for loop for one of the main array.

Java enhanced for loop syntax is as follows:

for (declaration statement: expression) {
    // Code sentence 
}

Disclaimer statement: declare a new local variable, and the type of the variable must match the type of array elements. Loop whose scope is defined in the block, and its value is equal to the array element at that time.

Expression: Expression is the name of the array to be accessed, or method that returns the value of the array.

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         int[] list = {0, 1, 2, 3, 4};
 4 //        for (String i : list) {
 5 //            System.out.print(i);  // 编译错误
 6 //        }
 7         for (int i : list) {
 8             System.out.print(i); // 0 1 2 3 4
 9         }
10     }
11 }

break keyword

break or loop mainly 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.

public class HelloWorld {
    public static void main(String[] args) {
        int[] list = {0, 1, 2, 3, 4};
        for (int i : list) {
            if (i == 2) {
                break;
            }
            System.out.print(i); // 0 1
        }
    }
}

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. 

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         int[] list = {0, 1, 2, 3, 4};
 4         for (int i : list) {
 5             if (i == 2) {
 6                 continue;
 7             }
 8             System.out.print(i); // 0 1 3 4
 9         }
10     }
11 }

Nested loop

Loop circulating jacket, a plurality of columns generally use a plurality of rows, the outer row control, column inner control.

The implementation process: the outer loop to go once the inner loop to go all times

Recommendation: nesting level as possible (a group of up to 2 layer cycle), the number of instructions designed to have more problems.

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         // 九九乘法表
 4         for (int i = 1; i <= 9; i++) {
 5             for (int j = 1; j <= i; j++) {
 6                 System.out.print(i + "*" + j + "=" + j * i + "\t");
 7             }
 8             System.out.print("\n");
 9         }
10     }
11 }
12 /**
13  1*1=1
14  2*1=2    2*2=4
15  3*1=3    3*2=6    3*3=9
16  4*1=4    4*2=8    4*3=12    4*4=16
17  5*1=5    5*2=10    5*3=15    5*4=20    5*5=25
18  6*1=6    6*2=12    6*3=18    6*4=24    6*5=30    6*6=36
19  7*1=7    7*2=14    7*3=21    7*4=28    7*5=35    7*6=42    7*7=49
20  8*1=8    8*2=16    8*3=24    8*4=32    8*5=40    8*6=48    8*7=56    8*8=64
21  9*1=9    9*2=18    9*3=27    9*4=36    9*5=45    9*6=54    9*7=63    9*8=72    9*9=81
22  */

 

Guess you like

Origin www.cnblogs.com/weijiutao/p/10911853.html