Namespaces
Variants

C++ keyword: while

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

使用方法

  • while ループ:ループの宣言として
  • do-while ループ:ループの終了条件の宣言として

#include <iostream>
int main() noexcept
{
    int i{3};
    // 以下の 'while' ループ文は:
    // 1. (条件) 変数 'i' の値がゼロより大きいかチェックし
    //            そうでない場合、この時点でループ実行を終了する
    //            変数 'i' を後置デクリメントする(値を1減らす)
    // 2. (文)   変数 'i' の現在値を stdout に出力する
    // 3.            ポイント1(条件)に戻る
    while (i --> 0)     // condition: i-- > 0
        std::cout << i; // statement: std::cout << i;
}

出力:

210

関連項目

(C++17以降)
(C++23以降)
(C++20以降)