C++ while Keyword

The while keyword in C++ is used to define a loop that repeatedly executes a block of code as long as a specified condition evaluates to true. The condition is checked before each iteration, making the while loop a precondition loop.

It is commonly used when the number of iterations is not known in advance and depends on a dynamic condition.


Syntax

</>
Copy
while (condition) {
    // Code to execute as long as condition is true
}
condition
A Boolean expression that determines whether the loop continues to execute. The loop runs while this condition evaluates to true.

Examples

Example 1: Counting with a while Loop

This example demonstrates a basic while loop that counts from 1 to 5.

</>
Copy
#include <iostream>
using namespace std;
int main() {
    int count = 1;
    while (count <= 5) {
        cout << "Count: " << count << endl;
        count++;
    }
    return 0;
}

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Explanation:

  1. The loop initializes with count = 1.
  2. The condition count <= 5 is checked before each iteration.
  3. The loop prints the current value of count and increments it by 1.
  4. When count becomes greater than 5, the condition evaluates to false, and the loop terminates.

Example 2: Infinite Loop

This example demonstrates an infinite while loop that runs indefinitely until explicitly terminated.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int count = 0;

    while (true) {
        cout << "Loop iteration: " << count << endl;
        count++;

        if (count == 5) {
            cout << "Breaking the loop." << endl;
            break; // Exit the loop
        }
    }

    return 0;
}

Output:

Loop iteration: 0
Loop iteration: 1
Loop iteration: 2
Loop iteration: 3
Loop iteration: 4
Breaking the loop.

Explanation:

  1. The while (true) condition ensures that the loop runs indefinitely.
  2. The loop prints the current iteration count and increments it by 1.
  3. The if statement checks if count == 5 and uses break to terminate the loop.
  4. Once the loop is terminated, the program exits the while loop.

Example 3: Using a while Loop for Input Validation

This example demonstrates how to use a while loop to validate user input.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int number;

    cout << "Enter a number between 1 and 10: ";
    cin >> number;

    while (number < 1 || number > 10) {
        cout << "Invalid input. Try again: ";
        cin >> number;
    }

    cout << "You entered: " << number << endl;

    return 0;
}

Output:

Enter a number between 1 and 10: 15
Invalid input. Try again: 7
You entered: 7

Explanation:

  1. The program prompts the user to enter a number between 1 and 10.
  2. The while loop checks if the input is out of range (< 1 || > 10).
  3. If the input is invalid, the loop prompts the user to try again.
  4. When the user enters a valid number, the loop exits, and the program displays the valid input.

Key Points to Remember about while Keyword

  • The while loop executes a block of code as long as the specified condition is true.
  • The condition is evaluated before each iteration, making while a precondition loop.
  • If the condition is false at the start, the loop does not execute even once.
  • It is commonly used for scenarios where the number of iterations is not known in advance.
  • The break and continue statements can be used to control the flow of the loop.