while
loopGeneral 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 | ||||||||||||||||
| ||||||||||||||||
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 | ||||||||||||||||
Conditionally executes a statement repeatedly.
Contents |
attr (optional)while ( condition) statement | |||||||||
attr | - | (since C++11) any number ofattributes |
condition | - | acondition |
statement | - | astatement (typically a compound statement) |
Acondition can either be anexpression or asimple declaration.
| (since C++26) |
When control reaches condition, the condition will yield a value, which is used to determine whetherstatement will be executed.
Ifcondition is an expression, the value it yields is the the value of the expression contextually converted tobool. If that conversion is ill-formed, the program is ill-formed.
Ifcondition is a simple declaration, the value it yields is the value of the decision variable (see below) contextually converted tobool. If that conversion is ill-formed, the program is ill-formed.
The declaration has the following restrictions:
| (until C++11) |
| (since C++11) |
The decision variable of the declaration is the declared variable.
Structured binding declarationThe declaration has the following restrictions:
The decision variable of the declaration is the invented variableeintroduced by the declaration. | (since C++26) |
Awhile statement is equivalent to
/* label */:
| |||||||||
Ifcondition is a declaration, the variable it declares is destroyed and created with each iteration of the loop.
If the loop needs to be terminated withinstatement, abreak statement can be used as terminating statement.
If the current iteration needs to be terminated withinstatement, acontinue statement can be used as shortcut.
Regardless of whetherstatement is a compound statement, it always introduces ablock scope. Variables declared in it are only visible in the loop body, in other words,
while(--x>=0)int i;// i goes out of scope
is the same as
while(--x>=0){int i;}// i goes out of scope
As part of the C++forward progress guarantee, the behavior isundefined if a loop that is not atrivial infinite loop(since C++26) withoutobservable behavior does not terminate. Compilers are permitted to remove such loops.
#include <iostream> int main(){// while loop with a single statementint i=0;while(i<10) i++;std::cout<< i<<'\n'; // while loop with a compound statementint j=2;while(j<9){std::cout<< j<<' '; j+=2;}std::cout<<'\n'; // while loop with a declaration conditionchar cstr[]="Hello";int k=0;while(char c= cstr[k++])std::cout<< c;std::cout<<'\n';}
Output:
102 4 6 8Hello
C documentation forwhile |