Spec-Zone.ru › C++

Ключевое слово C++: for

Использование

  • for цикл: в качестве объявления цикла
  • диапазонный цикл for: в качестве объявления цикла
(начиная с C++11)

Пример

#include <iostream>

int main() noexcept
{
    // The following 'for' loop statement:
    // 1. (init-statement) Declares an integer named 'i' and initializes it with value '0'.
    // 2. (condition)      Checks if i is less than 3 and if not, ends the loop execution.
    // 3. (statement)      Writes out a current value of the integer 'i' to the stdout.
    // 4. (expression)     Pre-increments the integer 'i' (increases its value by 1).
    // 5.                  Returns to the point 2 (condition).

                                // init-statement: int i{0};
                                // condition:      i < 3
    for (int i{0}; i < 3; ++i)  // expression:     ++i
        std::cout << i;         // statement:      std::cout << i;
}

Вывод:

012

См. также

  • if оператор: if, else
  • constexpr if оператор: constexpr (оператор constexpr if)
(начиная с C++17)
  • consteval if оператор: consteval (оператор consteval if)
(начиная с C++23)
  • switch оператор: switch, case
  • default (в качестве объявления метки case) и т. д.: default
  • goto оператор: goto
  • continue оператор: continue
  • break оператор: break
  • return оператор: return
  • co_return оператор (возврат из сопрограммы): co_return
(начиная с C++20)
  • do-while цикл и while цикл: do, while

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/keyword/for

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API