C++ While Loop is used to execute a code block repeatedly in a loop based on a condition.A while loop is a control statement using which the programmer can give instructions to the computer to execute a set of statement repeatedly as long as specified condition is satisfied. Ones the specified condition is false control comes out of the loop.
statement(s); //block of code
How while Loop Works in C++.
The initialization statement is executed first
Then, the test expression is evaluated. If the test expression is evaluated to false, then the while loop gets terminated and the control comes out of the while loop.
if the test expression is evaluated to true,The statements defined inside the while loop will repeatedly execute until the given condition fails.
After successful execution of statements inside the body of the while loop, the update expression is executed.
Again the test expression is evaluated.
Q.) W.A.P to Print numbers from 1 to n using while loop in C++ language ?
#include < iostream>
using namespace std;
int n, i = 1;
cout << "Enter number:" << endl;
cin >> n;
cout << endl;
while (i <= n)
cout << i << endl;