Spec-Zone.ru › C++

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

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

  • 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

См. также

  • 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
  • for цикл и цикл for на основе диапазона: for

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

Spec-Zone.ru

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