Loop structure,

Source: Internet
Author: User

Loop structure,

I. while statement format

Initialize the conditional expression;
While (condition expression) // when the condition is true, the statements in the loop are always executed, and the endless loop is executed. Therefore, you generally need to control the conditions {// execute the statement ;}

Ps: use ctrl + c to stop the endless loop under doc

Ii. do whle statement format

Do {// execution statement;} while (Condition Statement); // when do while has an English semicolon, it indicates that do while ends

Ps:
While first judge the condition, only when the condition is true to execute the loop body
Do while first executes the loop body, then judges the condition, the condition is true, and then continues to execute the loop body
That is, do while will execute a loop body no matter whether the condition is true or not.

3. for Structure

For (initial expression, cyclic condition expression, and cyclic Operation expression) {// cyclic statement ;}
Example of the for Loop execution sequence

For (int x = 0; x <6; x ++)
{
System. out. println ("x =" + x );
}

First, execute the for statement to determine whether the conditions are true and then perform a loop.
1. When int x = 6 is executed, the memory space is opened. The value is defined as x and the value is 0. The initialization expression is only executed once.
2. If x <6 is true, the output statement is executed.
3. After the output statement is executed, execute x ++.
4. Execute the "x <6" command to determine whether the statement is true. The result is "Step 2" and "Step 3". If the statement is false, the loop ends, and the space occupied by x is released.

 

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.