Summary of cyclic classification in C # language

Source: Internet
Author: User

In the C # language, loops are divided into 4 main types: While loop, do While loop, for Loop, Foeach Loop. Below I will classify the summary of the loop statement.

1. While loop:

   If the loop condition is true, the loop body is executed, and after the loop body is executed, the loop is judged if the condition is true, and if it is true, then the cycle is executed, and then the condition is true, so it goes on. The loop will not end until the result of the loop condition is false.
It is worth noting that the loop condition can be a value, a variable, an expression, but must be of type bool .
Code format:                      //code interpretation: while (loop condition)                   //If the loop condition is true, the loop body is executed, after the loop body is executed, the condition is judged true, and if true, then the loop problem is executed
{                                //Then determine if the condition is true, so keep going, knowing that the result of the loop condition is false, it will end the loop.
Circulation body;                           
}

For example: In the console output of 1000 can be divisible by 3

int  num=1;   The current number while (num<=1000) {    if (num%3==0)   {        Console.WriteLine (num):   }   num++;}

The above code can also be optimized to:

int  Num=3;while (num<1000) {    Console.WriteLine (num);    Num +=3;}

2. Do While loop:

Executes the loop body first, then determines whether the loop condition is satisfied, executes the loop body again if satisfied, and then determines whether the condition is satisfied, knowing that the condition is not satisfied before ending the loop.
It is important to note that the difference between a do and a while statement is that the do-while statement first executes the loop body and then judges the condition, while the while statement first judges the condition and then executes the loop body.

Code format: do{    loop body}while (cyclic condition);

For example: the console prompts the user to enter a positive number and then outputs the digit, which needs to be re-entered if the user is not entering positive numbers.

The while statement code is as follows: Console.Write ("Please enter a positive number:");d ouble number =double. Parse (Console.ReadLine ()), while (number<=0) {    Console.Write ("Please enter a positive number:"), number    =double. Parse (Console.ReadLine ());}   Console.WriteLine ("The number you entered is:" +number);//do While statement code is as follows: Double number;do{      console.write ("Please enter a positive numbers:");      Number =double.parse (Console.readlin ());} while (number<=0); Console.WriteLine ("The number you entered is:" +number);

Visible, in this case, the Do While statement is slightly more concise than the while statement.

3. For loop

First run the expression 1, determine whether the loop condition is true, if true, then execute the loop body, run after the execution of the expression 2, and then in judging the loop condition .... The loop will end until the loop condition is false.

Where expression 1 can be any code and is bound and executed only once; Expression 2 can be any code that executes after the loop body executes.

Code format for (expression 1; loop condition; expression 2) {loop-    body}

Example: Output 100 in the console hello.

for (int i=0;i<100;i++) {    Console.WriteLine ("Hello");}

The For loop is the most widely used and most extensive loop in the C # language.

4. Foreach Loop

From an array or collection, take each item's data in turn, and assign the data to the loop variable each time the data is fetched, and run the loop body once each assignment.

This loop is special, it can only be used to iterate over an array or collection, and the loop is a read-only loop and cannot change the array or collection in the loop body.

Code format: foreach (data type variable in array or collection) {    loop body}

Example: A collection of lists of type int is known, and the variable name is numbers, which requires that each item in the collection be output sequentially.

The For loop is represented as follows: for (int i=0;i<numbers,length;i++) {     Console.WriteLine (numbers[i]);} The foreach representation is as follows: foreach (int item in numbers) {    Console.WriteLine (item);}

Although the two are not much different from the code, the foreach statement has a higher operational efficiency.

Summary of cyclic classification in C # language

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.