fastread homefastrread library fastread menu

C++ : while and do...while Loop

Tutorial by:Manisha Dubey      Date: 2016-06-18 00:00:50

❰ Previous Next ❱

In computer programming, loop cause a certain piece of program to be executed a certain number of times. Consider these scenarios:

  • You want to execute some code/s certain number of time.
  • You want to execute some code/s certain number of times depending upon input from user.

These types of task can be solved in programming using loops.

There are 3 types of loops in C++ Programming:

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

C++ while Loop

Syntax of while Loop

while (test expression) {
     statement/s to be executed.  
}

How while loop works in C++ Programming?

The while loop checks whether the test expression is true or not. If it is true, code/s inside the body of while loop is executed,that is, code/s inside the braces { } are executed. Then again the test expression is checked whether test expression is true or not. This process continues until the test expression becomes false.

Flowchart of while Loop in C++

Flowchart of while loop in C++ Programming

Example 1: C++ while Loop

C++ program to find factorial of a positive integer entered by user. (Factorial of n = 1*2*3...*n)

#include <iostream>
using namespace std;

int main() {
    int number, i = 1, factorial = 1;
    cout<< "Enter a positive integer: ";
    cin >> number;
    
    while ( i <= number) {
        factorial *= i;      //factorial = factorial * i;
        ++i;
    }

    cout<<"Factorial of "<<number<<" = "<<factorial;
    return 0;
}

Output

Enter a positive integer: 4
Factorial of 4 = 24

 

 
 

In this program, user is asked to enter a positive integer which is stored in variable number (supposed user entered 4). Here is the working of while loop in this program:

  1. Initially, i = 1, test expression i <= number is true, 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.

C++ do...while Loop

The do...while Loop is similar to while loop with one very important difference. In while loop, check expression is checked at first before body of loop but in case of do...while loop, body of loop is executed first then only test expression is checked. That's why the body of do...while loop is executed at least once.

Syntax of do...while Loop

do {
   statement/s;
}
while (test expression);

How do...while loop works?

The statement/s inside body of loop is executed at least once, that is, the statement/s inside braces { } is executed at least once. Then the test expression is checked. If the test expression is true, the body of loop is executed. This process continues until the test expression becomes false. Since the body of loop is placed before the test expression in do...while loop, the body of loop is executed at least once.

Flowchart of do while loop in C++ programming

Example 2: do...while Loop

C++ program to add numbers entered by user until user enters 0.

#include <iostream>
using namespace std;

int main() {
    float number, sum = 0.0;
    
    do {
        cout<<"Enter a number: ";
        cin>>number;
        sum += number;
    }
    while(number != 0.0);

    cout<<"Total sum = "<<sum;
    
    return 0;
}

Output

Enter a number: 4.5
Enter a number: 2.34
Enter a number: 5.63
Enter a number: 6.34
Enter a number: 4.53
Enter a number: 5
Enter a number: 0
Total sum = 28.34

❰ Previous Next ❱


C++

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website