C++ while and do…while loop – with Examples

In this tutorial, with the aid of a few examples, we will learn how to use the while and do…while loop in C++ programming.

while loop

In computer programming, loops are used to run a block of code repeatedly.

Let’s say, for illustration, that we want to display a message 100 times. Then we can use a loop rather than writing the print statement 100 times.

That was just a simple illustration; by using loops effectively, we can make our programs much more sophisticated and efficient.

There are 3 types of loops in C++.

  1. for loop
  2. while loop
  3. do...while loop

We studied the C++ for loop in the previous tutorial. Here, we’ll learn about while and do…while loops.


C++ while loop

The while loop executes a block of code repeatedly while checking whether a given condition is true:

The syntax of the while loop is:

while (condition) {
    // body of the loop
}

Flowchart of while Loop

while loop
Flowchart of C++ while loop
Example 1: Display Numbers from 1 to 5
#include <iostream>
using namespace std;
int main() {
  int i = 0;
  while (i < 5) {
    cout << i << "\n";
    i++;
  }
  return 0;
}
OUTPUT:
0
1
2
3
4
Example 2: Sum of Positive Numbers Only
// program to find the sum of positive numbers
// if the user enters a negative number, the loop ends
//geektocode.com
#include <iostream>
using namespace std;
int main() {
    int number;
    int sum = 0;
    // take input from the user
    cout << "Enter a number: ";
    cin >> number;
    while (number > 0) {
        // add all positive numbers
        sum += number;

        // take input again if the number is positive
        cout << "Enter a number: ";
        cin >> number;
    }
    // display the sum
    cout << "\nThe sum is " << sum << endl;
    
    return 0;
}
OUTPUT:
Enter a number: 5254
Enter a number: 4
Enter a number: 5
Enter a number: 0

The sum is 5263

C++ do…while loop

while loop is a subset of the do...while loop. This loop will run the code block once before determining whether the condition is true; if it is, it will continue to run the loop indefinitely.

Its syntax is:

do {
   // body of loop;
}
while (condition);

Flowchart of do…while Loop

while loop
Flowchart of C++ do…while loop
Example 1: Display Numbers from 1 to 5
// C++ Program to print numbers from 1 to 5
//geektocode.com

#include <iostream>
using namespace std;
int main() {
    int i = 1; 
    // do...while loop from 1 to 5
    do {
        cout << i << " ";
        ++i;
    }
    while (i <= 6);
    return 0;
}
OUTPUT:
1 2 3 4 5 6
Example 2: Sum of Positive Numbers Only
// program to find the sum of positive numbers
// If the user enters a negative number, the loop ends
// the negative number entered is not added to the sum

#include <iostream>
using namespace std;
int main() {
    int number = 0;
    int sum = 0;
    do {
        sum += number;

        // take input from the user
        cout << "Enter a number: ";
        cin >> number;
    }
    while (number > 0); 
    // display the sum
    cout << "\nThe sum is " << sum << endl;
    return 0;
}
OUTPUT:
Enter a number: 4
Enter a number: 41
Enter a number: 41
Enter a number: 0
The sum is 86

The do…while loop in this case keeps going until the user types a negative value. The loop ends when the value is negative; the negative value is not added to the sum variable.

OUTPUT 2:
Enter a number: -6
The sum is 0.

Leave a Reply

Your email address will not be published. Required fields are marked *