while Loop in C++

 


The while loop in C++ is used to execute a block of code repeatedly as long as a given condition is true. The loop continues until the condition becomes false.


1️⃣ Syntax of while Loop:

while (condition) {
    // Code to be executed
}
  • condition: A boolean expression that is checked before each iteration.
  • The loop will execute the code inside the curly braces {} as long as the condition evaluates to true.

2️⃣ Example: Basic while Loop

Here’s an example where we print numbers from 1 to 5 using a while loop:

#include <iostream>
using namespace std;

int main() {
    int i = 1;

    // while loop to print numbers 1 to 5
    while (i <= 5) {
        cout << i << " ";
        i++;  // Incrementing i
    }

    return 0;
}

Explanation:

  • Initialization: The variable i is initialized to 1.
  • Condition: The loop continues as long as i <= 5.
  • Action: The current value of i is printed, and then i is incremented by 1.
  • Termination: The loop stops once i becomes greater than 5.

Output:

1 2 3 4 5

3️⃣ Example: Infinite while Loop

An infinite while loop runs indefinitely because the condition always evaluates to true. Here's an example:

#include <iostream>
using namespace std;

int main() {
    while (true) {
        cout << "This will run forever!" << endl;
    }

    return 0;  // This line will never be reached
}

Note:

  • You can exit an infinite loop using a break statement based on a certain condition (e.g., user input).

4️⃣ Example: Using while Loop with User Input

This example uses the while loop to keep asking the user for input until they enter 0:

#include <iostream>
using namespace std;

int main() {
    int num;

    cout << "Enter numbers (0 to stop): ";
    cin >> num;

    // while loop to keep asking for input until 0 is entered
    while (num != 0) {
        cout << "You entered: " << num << endl;
        cout << "Enter another number (0 to stop): ";
        cin >> num;
    }

    cout << "Program terminated." << endl;

    return 0;
}

Explanation:

  • The program will ask the user to enter a number and print the number until 0 is entered.
  • Once 0 is entered, the loop will terminate and the program will print "Program terminated."

Output:

Enter numbers (0 to stop): 3
You entered: 3
Enter another number (0 to stop): 5
You entered: 5
Enter another number (0 to stop): 0
Program terminated.

5️⃣ Key Points to Remember:

  • The while loop checks the condition before each iteration. If the condition is false initially, the code inside the loop will not execute.
  • Be cautious with infinite loops. Always make sure the loop has a condition that will eventually become false to avoid unwanted behavior.

6️⃣ Common Use Cases for while Loops:

  • Repeating a task until a condition is met (e.g., input validation, menu navigation).
  • Simulating a countdown or timer until a specified time is reached.
  • Searching for an element in a list or array until it's found.

Would you like to see more advanced examples with while loops, like nested loops or using it with arrays?

Post a Comment

0 Comments