Do – while loop

Estimated reading: 3 minutes 67 Views
The do-while loop in C++ is a control statement that allows you to execute a block of code at least once, and then it evaluates a condition. If the condition is true, the code block is executed again. The code block is executed repeatedly until the condition evaluates to false.

The general syntax of a do-while loop is as follows:

do {
  // code to execute
} while (condition);

The condition can be any Boolean expression. The do-while loop executes the code block at least once, and then it evaluates the condition. If the condition is true, the code block is executed again. The code block is executed repeatedly until the condition evaluates to false.

Here is an example of a do-while loop:

#include <iostream>

using namespace std;

int main()
{
    int c = 0, num = 0, sum = 0;
    do
    {
        cout << "Enter a number: ";
        cin >> num;
        sum += num;
        c++;
    }
    while (c <= 4);
    cout << "sum = " << sum << endl;
    return 0;
}

This code prompts the user to enter four numbers and then prints the sum of those numbers to the console. The program uses a do-while loop to ensure that the user enters at least one number, even if they enter an invalid number the first time.

Here is a step-by-step explanation of what happens when you run this code:

  1. The compiler declares the cnum, and sum variables and initializes them to the values 0, 0, and 0, respectively.
  2. The do statement begins the do-while loop.
  3. The cout << "Enter a number: "; statement prints the message “Enter a number: ” to the console.
  4. The cin >> num; statement reads a number from the user and stores it in the num variable.
  5. The sum += num; statement increments the value of the sum variable by the value of the num variable.
  6. The c++; statement increments the value of the c variable by 1.
  7. The while (c <= 4); statement evaluates the condition of the do-while loop. If the c variable is less than or equal to 4, the loop will continue to iterate. Otherwise, the loop will terminate.
  8. If the condition of the loop is true, the code block inside the loop is executed.
  9. Steps 3-7 are repeated until the condition of the loop is false.
  10. After the loop has terminated, the program prints the message “sum = ” to the console, followed by the value of the sum variable.

Here is an example of the output of this program:

Enter a number: 10
Enter a number: 20
Enter a number: 30
Enter a number: 40
sum = 100

Do-while loops can be used to solve a variety of problems, such as:

  • Validating user input
  • Checking for a certain condition to be met
  • Performing a task a certain number of times

The differences between while loop and do – while loop:

Feature While loop Do-while loop
Condition is checked Before the code block is executed. After the code block is executed.
Guarantee of execution The code block is not guaranteed to be executed at least once. The code block is guaranteed to be executed at least once.

Table explanation

  • While loop: The while loop checks the condition before the code block is executed. If the condition is true, the code block is executed. The code block is executed repeatedly until the condition evaluates to false.
  • Do-while loop: The do-while loop checks the condition after the code block is executed. The code block is executed at least once, regardless of the value of the condition. The code block is executed repeatedly until the condition evaluates to false.
Share this

Do – while loop

Or copy link

CONTENTS
English