名稱空間
變體
操作

C++ 關鍵詞: while

來自 cppreference.com
< cpp‎ | 關鍵字
 
 
C++ 語言
 
 

[編輯] 用法

  • while 迴圈: 作為迴圈的宣告
  • do-while 迴圈: 作為迴圈終止條件的宣告

[編輯] 示例

#include <iostream>
 
int main() noexcept
{
    int i{3};
 
    // The following 'while' loop statement:
    // 1. (condition) Checks if value of the variable 'i' is greater than zero
    //                and if not, ends the loop execution with end of this point.
    //                Post-decrements the variable 'i' (decreases its value by 1).
    // 2. (statement) Writes out a current value of the variable 'i' to the stdout.
    // 3.             Returns to the point 1 (condition).
 
    while (i --> 0)     // condition: i-- > 0
        std::cout << i; // statement: std::cout << i;
}

輸出

210

[編輯] 參閱

(C++17 起)
(C++23 起)
(C++20 起)