do 运算符迭代代码块,直到条件查询等于 false。do-while 循环也可以通过 gotoreturnbreakthrow 语句中断。

do 关键字的语法是:

做{ 代码块; } while( condition );

例:

int i = 0;

do
{
    Console.WriteLine("Do is on loop number {0}.", i);
} while (i++ < 5);

输出:

“在循环编号 1 上执行操作。”
Do on on number number
“Do is on loop number 3.”
“Do is on loop number 4.”
Do on on number 5

while 循环不同,do-while 循环是 Exit Controlled 。这意味着 do-while 循环将至少执行一次其语句,即使条件第一次失败也是如此。

bool a = false;

do
{
    Console.WriteLine("This will be printed once, even if a is false.");
} while (a == true);