Loop Statement Summary (code in C # for example)

Source: Internet
Author: User

1. While loop

Code format:

 while (loop condition) {    // loop Body }

Flow chart:

Interpretation:

If the loop condition is true, the loop body is executed
After executing the loop body, determine if the condition is true
If true, then the loop body is executed
And then judge whether the condition is true, so it goes on, until the result of the loop condition is false, it will end the loop

Case:

Use the dead loop to get the correct information for user input

define variables; while(true) {Console.Write ("Prompt for user input:"); Variables=get user input; if(variable satisfies the requirement) { Break;//end the endless cycle    }    Else{Console.Write ("error message, press ENTER to continue");        Console.ReadLine ();    Console.clear (); }}//after the loop is over, the correct data must be saved in the variable

2.do while loop

Code format:

 Do {    // loop body }while (loop condition)

Flow chart:

Interpretation:

Perform a loop body first
It then determines if the loop condition is satisfied and executes the loop body again if satisfied
And then determine whether the condition is satisfied, until the condition is not satisfied, to end the loop

3.for Cycle

Code format:

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

Flow chart:

Interpretation:

Run an expression 1
To determine if the loop condition is true, if true, then execute the loop body, run the expression 2 after execution, and then judge the loop condition ...
The loop will not end until the loop condition is false

4.foreach Cycle

Code format:

foreach inch array or collection) {// loop Body }

Interpretation:

Interpretation:

From an array or collection, remove the data for each item in turn
Assigns the data to the loop variable every data is fetched
Run the loop body once each time you assign a value

Loop Statement Summary (code in C # for example)

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.