C++ while loop, syntax and use

C++ while loop is used to execute a block of code several times. Although we can do the same task using for loop in C++, but it is recommended to use while loop when the the number of iteration is not fixed. We recommend you to learn about for loop in C++ and then come to this article for better experience.

The syntax of while loop in C++ is given bellow;

variable initialization;

    // block of code to execute when the condition is true
    increment or decrement

Now, we will see a simple program using while loop to understand while loop in C++ language. Let’s try to understand through the given program.

Example program using while loop in C++

// example program using while loop in c++

#include <iostream>
using namespace std;

int main(){

    int j = 0;    // initialize variable

    while(j <= 10){    // condition to check
        cout << j << endl;
        j = j + 2;    // incrementing

    return 0;

Output of while loop program


Now we will discuss that how this above program works. See the table bellow where we have given the flow of this program.

How while loop work in this program?

Value of j j <= 10 Action Incrementing j
print j = 0
j = 2
print j = 2
j = 4
print j = 4
j = 6
print j = 6
j = 8
print j = 8
j = 10
print j = 10
j = 12
Loop break

However, we will see more example use of while loop in our C++ programming section.

Nested while loop in C++

We can nest one while loop inside another as like as for loop in C++. We can create different layers of our program using nested while loop.

Syntax of nested while loop ( how nested while loop looks like ):

int a = 1;

while(a < 5){
    // code to execute if a > 5
    int b = 1;
    while(b < 4){
        // code to execute when b > 4

The inner while loop will iterate 3 times for each iteration of the outer loop.

Although we need to use nested loop in many times, but it is not recommended to use nested loop if there are other ways to solve the problem. Because using nested loop increases the time to run the program.

Now let’s see an executable program using nested while loop in C++

C++ program using nested while loop

// example program of nested while loop in c++

#include <iostream>
using namespace std;

int main(){

    int x = 1;

    while(x < 5){
        cout << x << ". Outer loop\n";

        int y = 1;
        while(y < 4){
            cout << "\t" << y << ". Inner loop\n";



    return 0;

Output of nested while loop program

Infinite while loop in C++

The loop which will never stop i. e. the condition will never become false is called infinite loop. We can create an infinite while loop in various way.

If you write 1 inside of while loop then it will be an infinite while loop.

#include <iostream>
using namespace std;

int main(){

        cout << "Infinite while loop\n";

    return 0;

This program will never stop unless the memory of your computer is full. We can create an infinite while loop using other condition too. What we have to do is, to create a condition which will always be true.

Some condition to make an infinite while loop is given bellow;



while(5 < 6){}

while(7 > 3){}

while(2 != 3){}

// any other true condition inside while loop

Previous pageFor loop in C++

Next pagedo while loop in C++