While Loop In C++

Card image cap

While Loop ?

while loop checks whether the condition written in ( ) is true or not. If the condition is true, the statements written in the body of the while loop i.e., inside the braces { } are executed. Then again the condition is checked, and if found true, again the statements in the body of the while loop are executed. This process continues until the condition becomes false.


While loop is a most basic loop in C++. while loop has one control condition, and executes as long the condition is true. The condition of the loop is tested before the body of the loop is executed, hence it is called an entry-controlled loop.

Syntax :

  While (condition)
   {
      statement(s);
      Incrementation;
   }
Card image cap

Example 1:



Output :

   value of a: 10
   value of a: 11
   value of a: 12
   value of a: 13
   value of a: 14
   value of a: 15
    
Card image cap

Example 2:



Output :

   Enter a positive integer: 4
   Factorial of 4 = 24
Card image cap