📜  而在C#中循环

📅  最后修改于: 2021-05-29 19:59:08             🧑  作者: Mango

以编程语言循环是一种根据要评估的条件的结果多次执行一条语句或一组语句的方法。 while循环是C#中的一个入口控制循环。

测试条件在循环的开头给出,并且所有语句都会执行到给定的布尔条件满足为止,当条件变为false时,控件将退出while循环。

句法:

while (boolean condition)
{
   loop statements...
}

流程图:

在C锐化中循环

范例1:

// C# program to illustrate while loop
using System;
  
class whileLoopDemo {
    public static void Main()
    {
        int x = 1;
  
        // Exit when x becomes greater than 4
        while (x <= 4) {
            Console.WriteLine("GeeksforGeeks");
  
            // Increment the value of x for
            // next iteration
            x++;
        }
    }
}
输出:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks

范例2:

// C# program to illustrate while loop
using System;
  
class whileLoopDemo {
    public static void Main()
    {
        int x = 10;
  
        // Exit when x becomes less than 5
        while (x > 4) {
            Console.WriteLine("Value " + x);
  
            // Decrement the value of x for
            // next iteration
            x--;
        }
    }
}
输出:
Value 10
Value 9
Value 8
Value 7
Value 6
Value 5