Do While Loop in C++

The do/while loop, it simply works like while loop except the statement or block of code has to execute once, before the the condition insert into the expression is checking to see if its true. Then the loop will keep looping as long as the condtion is fulfilled.

Syntax:

do {
// code block to be executed
}
while (condition);

Let's take an example, using do/while loop:

//Do While loop
#include <iostream>
using namespace std;

int main(){
  int i = 0;
  do {
    cout << i <<"\n";
    i++;
  }
  while (i < 5);
  return 0;
}

The do/while loop is always executed once, even if the condition inserted into the expression is not true. Because the code block is executed before the condition is being tested.

Real World Example

The example below will delighted how to use whileloop to achieve "countdown" program

// While Loop
#include <iostream>
using namespace std;

int main(){
  int countdown = 5;
 do {
    cout << countdown <<"\n";
    countdown--;
  } while (countdown > 0);
  cout << "Happy New Year \n";

  return 0;
}

The below example, demonstrate how to find even number between 1 to 10 using whileloop (inclusively)

// While Loop
#include <iostream>
using namespace std;

int main(){
  int i = 0;
  do {
    cout << i <<"\n";
    i+= 2;
  }while (i <= 10);

  return 0;
}

The below example, demonstrate how to reverse sequence of numbers using whileloop:

// While Loop
#include <iostream>
using namespace std;

int main(){
  // A variable stored sequence of some specific numbers
  int numbers = 12345;

  // A variable to store the reversed numbers
  int revNumbers = 0;

  // function to implement reverse and reorder the numbers
  do {
    // Get the last number of 'numbers' and add it to 'revNumbers'
    revNumbers = revNumbers * 10 + numbers % 10;
    // Remove the last number of 'numbers'
    numbers /= 10;
  }while (numbers > 0);

  cout << "Reversed numbers: " << revNumbers << "\n";

  return 0;
}

The below example, using whileloop and ifstatement to demonstrating a guessing game, where the user has to guess a secret number between 1 and 10. The program will give hints if the guess is too low or too high.

// While Loop
#include <iostream>
using namespace std;

int main(){
  int secretNumber = 7;  // The number to guess
  int guess = 0;         // User's guess

  cout << "Guess the secret number (between 1 and 10): ";

  do { 
      cin >> guess; // Take user input

      if (guess < secretNumber) {
          cout << "Too low! Try again: ";
      } else if (guess > secretNumber) {
          cout << "Too high! Try again: ";
      } else {
          cout << "Congratulations! You guessed the correct number!\n";
      }
  }while (guess != secretNumber);// Keep looping until the correct number is guessed

  return 0;
}
  • The secret number is set to 7
  • The whileloop keeps looping until user enter 7
  • The ifstatement will check:
    1. If the guess is less than the secret number, it will print "Too low! Try again"
    2. If the guess is greater than the secret number, it will print "Too high! Try again"
    3. If the guess is equal to the secret number, it will print "Congratulations! You guessed the correct number!"

The while loop repeatedly executes a block of code as long as a given condition is true. The ifstatement inside the loop allows us to perform conditional checks.

The below is a simple example of how find Even or Odd number using while loop with combination with ifstatement to check the condition:

// While Loop
#include <iostream>
using namespace std;

int main(){
  int num = 1; // Start counting from 1

  do { 
      if (num % 2 == 0) { // Check if the number is even
          cout << num << " is Even\n";
      } else { // If not even, it's odd
          cout << num << " is Odd\n";
      }
      num++; // Increment the number
  }while (num <= 10); // Loop runs from 1 to 10
  return 0;
}
  • The whileloop runs while numless than or equal to 10
  • The ifcondition will check whether the num is even using its provided expression.
  • If numis even then it will print "num is Even" otherwise it will print "num is Odd".
  • The num++increase the value of numby 1 in each iteration.

Key Differences Between while and do...while:

  • The whileloops check the condition before running the code inside.
  • The do...whileloops run at least once before checking the condition.