Day04: cyclic structure (while, do-while, for)

Java looping constructs - while, do ... while, for

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

There are three main Java looping constructs:

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

Enhanced introduced for a major loop for the array in the Java5.

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.

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.

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.

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.

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.

grammar

break usage is very simple, it is a statement cycle structure:

break;

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.

grammar

continue the loop body is a simple statement:

continue;

 

Guess you like

Origin www.cnblogs.com/wsnb8/p/11306553.html