C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Saturday, 15 April 2017

C++ While Loop

                                       C++ While Loop


Loops are used in programming to repeat a specific block of code. 

There are 3 type of Loops  in C++ Programming :

  • for Loop
  • while Loop
  • do....while Loop

C++ while Loop


The syntex of a while loop is :











Where, testExpression is checked on each enty of the while loop.

How while loop works ? 

  • The while loop evaluates the test expression.
  •  If the test expression is true, codes inside the body of while loop is evaluated.
  • Then, the test expression is evaluated again. this process goes on the test expression is false.
  • When the test expression is false, while loop is terminated.


Flowchart of while Loop






















Example  : C++ while Loop :




































Output :









In this program , user is asked to enter a  positive integer which is stored in variable number.
Let's suppose, User entered 4.

Then , the while loop states executing the code. Here's ow while loop works:

1. Initially, i = 1 , test expression i <= number is true and factorial becomes 1.
2. Variable i is updated to 2, test expression is true, factorial becomes 2.
3. Variable i is updated to 3, test expression is true, factorial becomes 6.
4. Variable i is updated to 4, test expression is true, factorial becomes 24.
5. Variable i is updated to 5, test expression is false and while loop is terminated.






0 comments:

Post a Comment

Popular Posts

Recent Posts

Categories

Unordered List

Text Widget