While Loop in C++

Loops have a purpose to repeat a block of code in certain number of times or while a condition is fulfilled.

The while loop, it simply repeat a statement or block of code, if the condition insert into the expression is true.

Syntax:

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

Let's observe the below example, where the code will keep looping untill variable i is less than 5:

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

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

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;
  while (countdown > 5) {
    cout << countdown <<"\n";
    countdown--;
  }
  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;
  while (i <= 10) {
    cout << i <<"\n";
    i+= 2;
  }

  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
  while (numbers) {
    // Get the last number of 'numbers' and add it to 'revNumbers'
    revNumbers = revNumbers * 10 + numbers % 10;
    // Remove the last number of 'numbers'
    numbers /= 10;
  }

  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): ";

  while (guess != secretNumber) { // Keep looping until the correct number is guessed
      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";
      }
  }

  return 0;
}
  • The secret number is set to 7
  • The whileloop keeps looping until user enter 7
  • The The ifstatement will check:
    • If the guess is less than the secret number, it will print "Too low! Try again"
    • If the guess is greater than the secret number, it will print "Too high! Try again"
    • 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

  while (num <= 10) { // Loop runs from 1 to 10
      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
  }
  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.