`while` — C++ Keyword
`while` — C++ Keyword
The while keyword in C++: repeatedly executes a body as long as a condition is true.
`while` — C++ Keyword
The while keyword in C++: repeatedly executes a body as long as a condition is true.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
whileRepeatedly executes a statement as long as the controlling condition evaluates to true. The condition is checked before each iteration, so the body may execute zero times.
while (condition) statement
#include <print>
int main() {
int n = 1;
while (n <= 5) {
std::print("{} ", n);
++n;
}
// Output: 1 2 3 4 5
std::println();
// Reading until a sentinel
int sum = 0;
int val = 0;
// while (std::cin >> val && val != 0) { sum += val; }
(void)sum;
}
false on first evaluation the body never executes. Use do/while when at least one execution is required.while (true) { ... }.whileint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}