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.