While Loop in C++

Video Tutorial
FREE
While Loop thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

Loops are an indispensable part of programming. C++ While loop is a type of the entry controlled loop which initially checks the condition and if the condition is true, then execute the loop body. It is basically used when the count of iterations is not fixed.

Syntax

The C++ language provides the following syntax for the while loop:

Parts of the While Loop

  • Test Expression
  • Loop Body
  • Update Expression

Test Expression

The test expression acts as a gateway that tells the while loop whether to execute the loop body or not. It gives a boolean result that directs the c++ while loop. The while loop keeps on executing till the test expression returns true. As soon as the test expression returns false, it stops the execution.

Loop Body

This is a group of statements that include variables, functions, and so on. With the c++ while loop, code, and simple names can be printed, complex algorithms can be executed, or functional operations can be performed.

Update Expression

The update expression is used to update the variable used in the test expression. It is a part of the loop body and hence its execution occurs along with the loop body. If this expression gets wrong or missed then the loop may run infinitely.

How to Execute C++ while Loop?

  1. The program control enters the c++ while loop.
  2. The control initially reaches the test condition.
  3. The test condition is checked.
  4. The block of code inside the loop gets executed if the test condition returns true and the control immediately gets out of the loop if the test condition returns false.
  5. The control then executes the block of code present inside the while loop.
  6. The loop variable gets updated along or after the code execution.
  7. The control moves to STEP 2 till the test expression returns true.
  8. When the test condition returns false, the execution of the while loop ends.

Flow Diagram

Flow Diagram of While loop in C++

C++ While loop Example

Output

Examples

Example 1: Program to reverse a number using while loop in C++

output

Step-wise explanation of above code:

  • Step 1: Initialization of the number to be reversed in 'n'.
  • Step 2: Initialization of the output variable i.e. reverseOfn to 0.
  • Step 3: Checking the test condition ie. n > 0
  • Step 4: Entering in the while loop.
  • Step 5: Multiplying reverseOfn by 10 and adding the remainder of n divide by 10 to reverseOfn.
  • Step 6: Update the loop variable n by n/10.
  • Step 7: Print reverseOfn.
  1. First iteration: The value of reverseOfn becomes 4 and n becomes 123.

  2. Second iteration: The value of reverseOfn becomes 43 and n becomes 12.

  3. Third iteration: The value of reverseOfn becomes 432 and n becomes 1.

  4. Fourth iteration: The value of reverseOfn becomes 4321 and n becomes 0.

Example 2: Program to print first n numbers of Fibonacci series using while loop in C++

output

Step-wise explanation of above code:

  • Step 1: Initializing a=0 and b=1.
  • Step 2: Check if n is smaller than 1, if yes, then print Invalid input and return.
  • Step 3: Print 'a' and initialize the loop variable s=1.
  • Step 4: Checking the test condition s<n.
  • Step 5: Entering in the while loop.
  • Step 6: Print 'b' and add a and b and put the result in c.
  • Step 7: Update 'a' by 'b' and 'b' by 'c'.
  • Step 8: Increment the loop variable s by 1.

Nested while loops in C++

Nested c++ while loops are represented by while loops inside a while loop. There can be one or more than one while loops inside another while loop. The while loop that contains other while loops is often called the outer while loop or parent while loop. The while loops inside the outer while loop are often called inner or child while loop.

Example 3: C++ Nested While Loop Example

output

Infinite while loops in C++

Infinite c++ while loops are the never-ending loops. The test expression in such while loops always results true. Missing or wrong update expressions may cause infinite while loops to occur.

Example 4: C++ Infinite While Loop Example

output

Explanation: The update expression loop variable decreases every time and it is always going to be less than 5. Hence the loop will run infinitely.

Example 5: Displaying the elements of array using while loop

output

Conclusion

  • A loop is used to accomplish the job of executing a block of code recurrently.
  • Loops in C++ consist of two types i.e. entry and exit controlled loops.
  • C++ While loop is a type of the entry controlled loop which initially checks the condition and if the condition is true then execute the loop body.
  • No other loops can work if the number of iterations is not known except while loop.
  • Process of execution of while loop in C++: Checking test condition —> Execution of loop body —> Updation of the loop variable.
  • A while loop containing one or more than one while loop is known as a nested while loop.
  • A while loop in which the test expression never returns false is known as an infinite while loop.
  • Do-while loops are the exit controlled loops that initially executes the loop body and then check the test expression.