do 语句重复执行括在 {} 里的一个语句或语句块,直到指定的表达式计算为 false。在下面的示例中,只要变量 y 小于 5,do 循环语句就开始执行。
y
// statements_do.cs using System; public class TestDoWhile { public static void Main () { int x = 0; do { Console.WriteLine(x); x++; } while (x < 5); } }
0 1 2 3 4
与 while 语句不同,do 语句的体循环至少执行一次,与表达式的值无关。
有关更多信息,请参见 C# 语言规范中的以下各章节:
5.3.3.8 do 语句
8.8.2 do 语句