C++ | C Plus Plus While loop
C++ while Loop Statement
The while loop is an iterative control statement. A while loop repeatedly executes loop's statement(s) several times as a given condition is true. Execution of loop is terminated when the condition of while loop becomes false and execute code after the loop.Syntax
Working Process Of While Loop
Output:
while(condition){
statement(s);
}
- While loop first checks the condition applied.
- If the condition of a while loop is satisfied (true), statement(s) within the loop is executed. If condition of a while loop is not satisfied (false), program execution is terminated from loop and statement after the loop is executed.
- Repeatedly check the loop condition and execute according to step 2.
C++ while loop Example
This program prints counting from 1 to 10.#include <iostream>
using namespace std;
int main()
{
int i=1;
while (i<=10) {
cout<<i<<"\n";
i++;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10