While Loop

Estimated reading: 6 minutes 169 Views

The while loop in C++ is a control statement that allows you to execute a block of code repeatedly while a condition is true. The general syntax of a while loop is as follows:

while (condition) {
  // code to execute while the condition is true
}

The condition can be any Boolean expression. The while loop evaluates the condition and if it is true, the code block inside the while loop is executed. The code block inside the while loop is executed repeatedly until the condition evaluates to false.

Here is an example of a while loop:

#include <iostream>

using namespace std;

int main()
{
    while (true)
    {
        cout << " a word" <<endl;
    }
    
    return 0;
}

The previous code is a simple infinite loop in C++. It will print the phrase “a word” to the console forever, or until the program is terminated.

The while (true) statement creates an infinite loop. This means that the code block inside the loop will be executed forever, or until the program is terminated.

The cout << " a word" << endl; statement prints the phrase “a word” to the console. The endl; object at the end of the line inserts a newline character, which causes the next line of output to be printed on a new line.

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

  1. The compiler evaluates the condition of the while statement. Since the condition is always true, the code block inside the loop is executed.
  2. The compiler prints the phrase “a word” to the console.
  3. The compiler goes back to step 1 and repeats the process.

This process continues forever, or until the program is terminated.

To terminate the program, you can press Ctrl+C on your keyboard.

Here are some things to keep in mind about infinite loops:

  • Infinite loops can be useful for certain tasks, such as polling for input or waiting for a certain event to occur.
  • However, it is important to use infinite loops carefully, as they can also cause your program to crash if you are not careful.
  • For example, if you accidentally create an infinite loop that prints output to the console, your console window will quickly fill up with output and your program will become unresponsive.

To overcome these problems we should make 3 steps:

  1. declare a variable and initializes it to any value we want.
  2. add a condition to while statement.
  3. set a counter for our variable.

These steps are shown in the following example:

#include <iostream>

using namespace std;

int main()
{
    int c = 0;
    while (c < 5)
    {
        cout << " a word" <<endl;
        c++;
    }
    
    return 0;
}

int c = 0;, declares an integer variable named c and initializes it to the value 0.

The while (c < 5) statement creates a loop that will iterate as long as the value of the c variable is less than 5.

The cout << " a word" << endl; statement prints the phrase “a word” to the console. The endl; object at the end of the line inserts a newline character, which causes the next line of output to be printed on a new line.

The c++; statement increments the value of the c variable by 1.

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

  1. The compiler declares the c variable and initializes it to the value 0.
  2. The compiler evaluates the condition of the while statement. Since the condition is true, the code block inside the loop is executed.
  3. The compiler prints the phrase “a word” to the console.
  4. The compiler increments the value of the c variable by 1.
  5. The compiler goes back to step 2 and repeats the process.

This process continues until the value of the c variable is equal to 5. At that point, the condition of the while statement will evaluate to false and the loop will terminate.

Here is the output of this program:

a word
a word
a word
a word
a word

Here is another example:

#include <iostream>

using namespace std;

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

This code is a simple C++ program that prompts the user to enter four numbers and then prints the sum of those numbers to the console.

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 while (c < 4) statement creates a loop that will iterate as long as the value of the c variable is less than 4.
  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 = 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 compiler goes back to step 2 and repeats the process.

This process continues until the value of the c variable is equal to 4. At that point, the condition of the while statement will evaluate to false and the loop will terminate.

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

While loops are a powerful tool for controlling the flow of your C++ program. By understanding how to use them, you can write more efficient and reliable code.

Here are some tips for using while loops:

  • Make sure that the condition of the while loop will eventually evaluate to false. Otherwise, the while loop will run forever.
  • Use a break statement to exit the while loop early.
  • Use a continue statement to skip the rest of the current iteration of the while loop.
Share this

While Loop

Or copy link

CONTENTS
English