Latest web development tutorials

C # loop

Sometimes, you may need to perform several times the same piece of code. Under normal circumstances, the statement is the order of execution: the function of the first statement executed first, followed by a second statement, and so on.

Programming languages ​​that allow more complex execution paths of various control structures.

Loops allow us to repeatedly execute a statement or group of statements, the following is the general form in most programming languages ​​looping statement:

Loop structure

Type of cycle

C # provides the following cycle types. Click on the link to view the details of each type.

循环类型描述
while 循环 当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
for 循环 多次执行一个语句序列,简化管理循环变量的代码。
do...while 循环 除了它是在循环主体结尾测试条件外,其他与 while 语句类似。
嵌套循环 您可以在 while、for 或 do..while 循环内使用一个或多个循环。

Loop control statements

Loop control statements to change the normal sequence of execution. When performing a range of leave, all created in the target range will be automatically destroyed.

C # provides the following control statements. Click on the link to view the details of each statement.

控制语句描述
break 语句 终止loopswitch语句,程序流将继续执行紧接着 loop 或 switch 的下一条语句。
continue 语句 引起循环跳过主体的剩余部分,立即重新开始测试条件。

Infinite loop

If the condition is never false, the loop becomes an infinite loop.for circulation in the traditional sense it can be used to implement an infinite loop.Since the three expressions constitute any one cycle is not required, you can be certain conditional expression blank to form an infinite loop.

using System;

namespace Loops
{
    
    class Program
    {
        static void Main (string [] args)
        {
            for (;;)
            {
                Console.WriteLine ( "Hey I am Trapped!");
            }
 
        }
    }
} 

When the conditional expression does not exist, it is assumed to be true. You can also set an initial value and the increment expression, but under normal circumstances, programmers prefer to use for (;;) structure to represent an infinite loop.