In this tutorial, we will explore and learn about while loops in C++ and explain its usage with the help of example code. 

What are Loops?

In programming languages, loop is a functionality which allows us to run a sequence of instructions until a certain condition is matched. Generally, there are two common kinds of loops supported in almost every programming language; while loop and for loop.

Similar to other programming languages, C++ also offers the functionality of a while loop. In practical programming, loops are frequently used to achieve meaningful results out of the program. Without further delays let’s jump to learning while loop in C++.

C++ While Loop Syntax

In C++, we use the following syntax to execute the while loop.

while (your_condition)
{
  // the code chunk which will execute if your_condition is true.
}

Note that the while loop above will only run as long as your_condition evaluates to true. As soon as the condition evaluates to false, the loop will terminate.

Example

Following is a dummy example use case for while loop:

bool isInternetConnected = true;
while (isInternetConnected)
{
  keepLearningFromCodeZen();
}

In the above example, the function keepLearningFromCodeZen() will keep on executing until the value of the variable isInternetConnected is true. As soon as the value is set to false, the loop will terminate and the function will stop executing.

Let’s look at another real-life example where while loop can be used.

int counter = 0;
int result = 1;
while (counter < 5)
{
    result = result * 2;
    counter++;
}

The above code will run until the condition counter < 5 is true. The loop will run five times; starting from counter equal to 0 then 1, 2, 3 and 4, each time the result is multiplied by 2 and the new value is again saved in the same variable result. The loop will terminate as soon as the value of counter is equal to 5. So, at the end of program execution, the value inside the variable counter will be 5 whereas the value in the variable result will be 32. 

Can we create a never-ending program using while loop? Yes, we can! If we build up a scenario where there is no terminating condition, the while loop will keep on executing and the program will never come out of the loop. 

The following code depicts a dummy scenario where the program will always reside inside the loop and will never come out unless it is terminated forcefully.

while (true)
{
  // the code segment with no terminating condition
}

Terminating the While Loop Forcefully

Apart from the normal/routine conditions, while loops can also be terminated forcefully. C++ offers a keyword called break which terminates the loop at any point irrespective of the terminating condition.

bool isInternetConnected = true;
while (isInternetConnected)
{
  keepLearningFromCodeZen();
  break;
}

A quick refresher test! How many times does the function keepLearningFromCodeZen() execute in the above code snippet? Take a moment to think on your own… What is your answer? If your answer is one, then you are absolutely correct. Congratulations! The initial condition isInternetConnected is true and hence the program enters the while loop. The program will run the function as per the sequence and then encounters a break. As soon as the program sees the break keyword, it will immediately come out of the loop. 

Let’s test ourselves again! In the code below, how many times the function keepLearningFromCodeZen() execute?

bool isInternetConnected = true;
while (isInternetConnected)
{
  break;
  keepLearningFromCodeZen();
  
}

Correct answer: Zero

That’s all about while loops in C++. Hope it was fun learning about the implementations and use cases of while loop in one of the most popular programming languages out there. Don’t forget to do some hands-on exercises and apply the learning from here in a practical setting. 

Make sure to check our tutorial about conditionals (if, else if and else) in C++. Try figuring out some interesting applications where you can combine while loop and conditional statements to make interesting programs in C++.

If you wish to learn more about C++, you can check out our collection of C++ tutorials.