Breaking News

How to use while loop in C++

Loop in Programming is the process of repeating a specific block of code while a given condition is true. Generally codes are executed sequentially but there is a situation when you need to execute a block of code several number of times. A loop statement allows us to execute a statement or group of statements multiple times.
There are four types of Loop provided in C++, these are:
  1. For Loop
  2. While Loop
  3. Do While Loop
  4. Nested Loop
But through this article we will discuss only one loop which is While Loop, the rest we will discuss in the coming posts. 

While Loop is a Pre-Test Loop which means it test the condition before the execution of any statement. This loop can be used if you want to repeat the execution of some statements but you don't even know how many times loop should execute them, or some times you can use it depending on the user selection. The powerful of this loop compare to other, apart from to be pre-test loop but also it can be used in sentinel value. Sentinel is the special value that when it is used as input it terminate a loop. 

The below picture shows the general structure of the Loop.

In this structure there are four parts which are:
  • Start. Is the starting point of your loop where initial value is initialized.
  • Condition. Is the Boolean expression which has two results, either true or false.
  • Statement. Is the lists of statement that will be executed once the condition return to be true.
  • End. Is the end your loop, and this termination will be executed if and only if the condition return false.
While Loop Syntax.

     step value.

In this syntax of while loop there is the following meaning of each word.
  • while. Is the keyword that is used for looping (repetition).
  • condition. Is the Boolean expression that is either true or false. If the it is true, it will execute the statements below the loop but if it is false, it will terminate and exit.
  • statement. This can be more than one statement that you want to be executed if and only if the condition has true result.
  • step value. Is the number of steps (interval) from the initial value. I call it step value because it may increase( + ) or decrease ( - ) it depend to the nature of the problem that you solve.
From the description of the syntax, lets have one example that will elaborate the concept of this kind of loop.

Example: Write a program that will display odd numbers from 0 to 100 exclusive. Using while loop

To solve this kind of question you have to as yourself about the following.
  • What is initial value?
  • What is final value?
  • And how about step value?
  • What is my output after all?
When you trying to ask these questions by yourself, the mind is getting wider to get the solution. Now let us answer those questions as following.
  • What is initial value? Because, the question say that, I have to display odd numbers from 0 to 100 exclusive, here my initial value will be 0 and also my final value will be 100. So I have to write a loop that will repeat 100 times but in output I have to display a number from 1 to 99 because 0 and 100 has been excluded from my question
  • And how about step value? My step value can be increment ( + ) or decrement ( - ) it depend to the nature of the question, but from my question say that "I have to display odd numbers from 0 to 100 exclusive". If this is the case, I have to know that, when the number start from 0 to 100 they increase or decrease? The answer is "they increase", so if they increase, they increase by what interval? The answer is "I don't know". So if you don't know just take interval of 1, such that they will start from 0 1 2 3 4 5 ……
  • What is my output after all? My output should be an odd numbers, so from the series of my numbers (0 1 2 3 4 5 …..) how can I get an odd numbers (matter of simple calculation) just take number%2 != 0 odd number.
From the answer of my questions now the last part is to implement my answers in reality. The implementation will be:

#include <iostream>
using namespace std;
int main()
    int number=0;
    while (number <= 100)
        if(number%2 != 0)
            cout<<number<<" ";

I just one to explain only one thing from this peace of code because the rest ones have been discussed in previous posts (click here to learn an if statement)
(number%2 != 0). This condition has the following three parts.
  1. number%2. This mean that take the number divide by 2, what value will remain? Here we use the remainder/modulus operator ( % ) which it takes the remain value after division process. So if the number is 4 the remainder will be 0, if the number is 5 remainder will be 1.
  2. == . This is the relation operator. Click here learn get concept about these operators.
  3. 0 . This is value that is compared with the remain value by using comparison operator ( == ).
By this condition, the cout statement will be executed if and only if the result of condition will be true, and the condition to be true it must have same value of not zero ( 0>number<0 ) from left side ( number%2 ) and right side ( 0>number<0 ). From left side, only odd number will return value which is not zero, and that is the only thing that gives us the result of all odd numbers from our series of (0 1 2 3 4 5 ……..)

The output for this program will be

Example 2: (Concept of Sentinel) Write a program that will accept all positive numbers from the user, but when a user enter a negative number the program should terminate and display the sum of all numbers entered except the negative 1.

This kind of question you can't understand the number of iteration because you don't know after how many positive number user can enter a negative one. So such kind of question have the concept of sentinel where we will use it as a special value to terminate the loop.

So the implementation of this question is.

#include <iostream>
using namespace std;

int main()
    int number, sum=0;
    cout<<"Enter a number: ";
    while (number >= 0)
       cout<<"Enter a number: ";
    cout<<"Sum of positive number entered is "<<sum;

And the output of this program will be seen as the following in the image.
My input was 5, 78, 34, 9, 0, 21, 5 and -6. Here the sentinel value is -6.

We are done...!!!

I hope now you understand how to use While Loop in C++. Feel free to ask any question if you didn't understand then I can reply as faster as I can. 

Please comment and share with your friends. Don't miss the following post where we will continue to learn different things and application of new concepts.

Thank you for reading, be blessed....!!!


  1. I want to learn more about C++, I need books and tutorial recommendations.

    1. Thank you so much!! I'll put books here in this site soon as possible and about tutorials just follow us in our youtube channel @