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.