Three loop structures and loop nesting in Java

Source: Internet
Author: User

Loop structure: syntax, and execution order syntax:

while (Boolean type) {

Function statements

}

Execution order:

1 The value in the break parenthesis is true, and if true, the function statement is executed

2 line up the function statement, continue to determine the value in parentheses, if true, continue to execute the function statement

3 jumps out of the loop until the value in the parentheses is determined to be false

Precautions:

Write value in while parenthesis true dead loop

Write false error in while parenthesis: unable to access statement

Do-while: Syntax structure:

do{

Circular Function Statement C

Post-loop statement D (statement to control loops)

}while ();//cyclic judgment Statement B

Execution order:
    1. First execute the function code inside the do struct once
    2. First judgment while () result is ture
    3. Perform a Do loop body ....
    4. Second judgment while () result is ture
    5. Perform a Do loop body ....
    6. .......
    7. Nth time judgment while () result is flase Loop end
The difference between the and while:

1. Differences in grammatical structure

The 2.while statement is terminated after the test condition is executed before the statement condition is not met. Do While is the first to execute the statement re-test condition, the condition does not meet after termination

3.do while executing at least once

For loop: syntax structure:

For (Loop initialization statement A; Loop judgment statement B; post-loop statement d) {

Looping function statements

}

Execution order:

Loop Initialization Statement A: executes once

B-->true---->C---->d

B-->true---->C---->d

.....

B---End of >false cycle

Characteristics:

for (;;) {No functional statement} dead loop.

for (;;) After direct with function statement (1 innings) dead loop

for (;;) followed by two function statements, second inning error, for unreachable statements

Precautions:

When for (int I = 1; I <= 10; i++) I is a variable that belongs to the body of the structure and cannot be accessed externally

Loop Nesting: Concepts:

Loop nested loops also have loops that can be nested in multiple layers, and different loop structures can be nested with each other.

For example:

for (int i = 1;i<=5;i++) {   //control print line Count                          for (int k = 1;k<=5-i;k++) {   //control to print a line of content                                   System.out.print ("");                          }                          for (int j = 1;j<=i;j++) {   //control print a line of content                                   System.out.print ("*");                          }                          System.out.println ();                  }

Suggestions:
    1. The actual development does not nest too deep, in general case nested enough.
Loop control statement: Break: Features:

1. Loop structure end: does not equal the end of the method, the statement behind the for Loop struct will also run normally

2.break just terminates when the layer loops:

3. If you want to terminate the outer loop in the inner layer: you can loop an alias to the outer.

Syntax: with the break name; ---> End outer loop

Example:
Class breaktest{public    static void Main (strinf[] args) {          test:for (int i = 0; i<=5; i++) {              System.out.println (i);              for (int j = 0; J <= 5; j + +) {                  if (j = = 3) {break                      test;                  }      }}}

Continue: Features:

Skip the layer as the second cycle, continue the next cycle

Return: Features:

End method when used in a loop, indicating the end of the current method

Precautions:

The break statement, the Coutinue statement, and the return statement cannot be followed by a write statement. Error: Cannot access

Three loop structures and loop nesting in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.