C # loop syntax and their respective features

Source: Internet
Author: User

C # contains the following types of loops:

① While

② Do... while...

④ Foreach

 

No1.While Loop

Bool flag = true;

While (flag)

{

/* Do something */

If (Condition)

{

Flag = false;

}

}

The while LOOP checks the logical expression before each loop. The loop jump completely depends on the changes in the loop body. However, if the program has some problems in it, it will lead to an endless loop.

 

No2.do... while... loop

Similar to the while loop.

 

No3.for Loop

.

 

No4. foreach Loop

The foreach loop is mainly used to traverse the container type that implements IEnumerable. Both the array type and Common Containers implement this interface.

Foreach is preferred when designing to traverse containers or arrays.

However, the foreach statement has certain limitations, that is, it cannot assign values to the project, nor assign values to the internal members of the project through properties. However, it can call internal methods to assign values to internal members.

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.