C#语言 Do运算符
示例
do运算符遍历代码块,直到条件查询等于false。在do-while循环也可以通过一个被打断goto,return,break或throw语句。
do关键字的语法为:
do { code block; } while( condition );
例:
int i = 0; do { Console.WriteLine("Do is on loop number {0}.", i); } while (i++ < 5);
输出:
"Do is on loop number 1." "Do is on loop number 2." "Do is on loop number 3." "Do is on loop number 4." "Do is on loop number 5."
与while循环不同,do-while循环是ExitControlled。这意味着,即使条件第一次失败,do-while循环也将至少执行一次其语句。
bool a = false; do { Console.WriteLine("即使a为false,也将打印一次。"); } while (a == true);