The loop structure in Java

Source: Internet
Author: User

1.while Cycle Structure

  Grammar:

 while (loop condition) {  // loop Action }

  While loop structure Flowchart:

  Example:

1 int i = 1; 2  while (i <=) {3 System.out.printlen ("good study"); 4 i + +; 5 }

Note: The content in the parentheses after the keyword while is the while loop condition. A loop condition is an expression whose value is a Boolean type, that is, ture or false. The statements in {} are collectively referred to as circular operations, also known as loop bodies.

The structure of the while loop structure is executed in the following order:

1) Declare and initialize the loop variable

2) determine if the loop condition is satisfied, if satisfied, then perform a cyclic operation, or exit the loop

3) After performing the loop operation, judge the loop condition again, decide to continue the loop or exit the loop

A feature of the while loop: first judgment, then execution.

2. Do-while Cycle

Grammar

 Do {  // loop Action }  while (cyclic conditions);

Flowchart of the Do-while cycle

Example:

1 int i = 1; 2  Do {3 System.out.println ("good study"); 4 i++; 5 }while (i <= 100);

Note: The Do-while loop structure ends with a semicolon.

The execution order of the Do-while loop is generally as follows:

1) Declare and initialize the loop variable

2) perform the cycle operation again

3) Determine the loop condition, if the loop satisfies the condition, then the loop continues execution, otherwise exits the loop

Characteristics of Do-while cycle: first execution, after judgment.

3.for Cycle

Grammar:

 for (expression 1; expression 2; expression 3) {  // loop Body }

Expression 1: An assignment statement

Expression 2: Conditional statement

Expression 3: An assignment statement

The loop structure in Java

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.