C++ do 关键字
示例
以下循环将始终至少执行一次,即使条件为 false,因为代码块在条件被测试之前执行
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
定义和用法
The do
keyword is used together with while
to create a do/while loop.
只要指定的条件为真,while
循环就会重复执行一段代码。
do/while
循环是 while
循环的一种变体。这个循环会先执行一次代码块,然后检查条件是否为真,只要条件为真,它就会重复循环。
注意:不要忘记增加条件中使用的变量,否则循环将永远不会结束!
相关页面
Read more about the do/while loop in our C++ Do/While Loop Tutorial.