Namespaces
Variants

C++ keyword: for

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

사용법

  • for 루프: 루프의 선언으로서
  • 범위 기반 for 루프: 루프의 선언으로서
(C++11부터)

예제

#include <iostream>
int main() noexcept
{
    // 다음 'for' 루프 구문:
    // 1. (초기화문) 'i'라는 이름의 정수를 선언하고 값 '0'으로 초기화합니다.
    // 2. (조건문)    i가 3보다 작은지 확인하고, 그렇지 않으면 루프 실행을 종료합니다.
    // 3. (실행문)    정수 'i'의 현재 값을 stdout에 출력합니다.
    // 4. (표현식)    정수 'i'를 전위 증가시킵니다 (값을 1 증가시킵니다).
    // 5.             포인트 2(조건문)로 돌아갑니다.
                                // 초기화문: int i{0};
                                // 조건문:      i < 3
    for (int i{0}; i < 3; ++i)  // 표현식:     ++i
        std::cout << i;         // 실행문:      std::cout << i;
}

출력:

012

참고 항목

(C++17부터)
(C++23부터)
(C++20 이후)