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'의 값이 0보다 큰지 확인하고
    //                그렇지 않으면 이 지점에서 루프 실행을 종료합니다.
    //                변수 '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부터)