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;

while(condition){
    
    // 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

while-loop-in-c-program-using-while-loop-in-c

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
0
true
print j = 0
j = 2
2
true
print j = 2
j = 4
4
true
print j = 4
j = 6
6
true
print j = 6
j = 8
8
true
print j = 8
j = 10
10
true
print j = 10
j = 12
12
false
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
        
        b++;
    }
    
    a++;
}

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";
            y++;
        }

        x++;

    }

    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(){

    while(1){
            
        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(true){}

while(!false){}

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++