/* */ Click to Join Live Class with Shankar sir Call 9798158723

While Loop in C++

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.

Syntex :-

statement(s); //block of code

How while Loop Works in C++.

  • step 1:-
    The initialization statement is executed first
  • step 2:-
    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.
  • step 3:-
    if the test expression is evaluated to true,The statements defined inside the while loop will repeatedly execute until the given condition fails.
  • step 4:-
    After successful execution of statements inside the body of the while loop, the update expression is executed.
  • step 5:-
    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 main()
    int n, i = 1;
    cout << "Enter number:" << endl;
    cin >> n;
    cout << endl;

    while (i <= n)
        cout << i << endl;

    return 0;
Enter number 8