Dremendo Tag Line

while Loop in C++ Programming

Loops in C++

In this lesson, we will understand what is while loop in C++ programming with some examples.

What is while Loop

while loop is used in situations where we do not know the exact number of iterations of loop beforehand. while loops continue to loop as long as a test expression is true. The while loop is also called an Entry Controlled loop because the test expression is tested before entering the loop body.

Syntax of while Loop

initialization_expression;
while(test_expression)
{
    // body of the loop
    // statements we want to execute
    update_expression;
}

Example

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    // initialization expression
    int i = 1;

    // test expression
    while(i<=5)
    {
        cout<<"Hello World\n";

        // update expression
        i=i+1;
    }
    return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World

Just like for loop in while loop also, we initialize the loop variable before starting the while loop. After initialization, we will write our test expression inside the parenthesis () of while loop. If the test expression evaluates to true, then the body of the while loop will execute and, the update expression will update the loop value. This process continues until the test expression evaluates to false. When the test expression evaluates to false the while loop terminates.

Example 1

C++ program to print the numbers from 1 to 10 on the screen using while loop.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=1;
    while(i<=10)
    {
        cout<<i<<endl;
        i=i+1;
    }
    return 0;
}

Output

1
2
3
4
5
6
7
8
9
10

In the above example, we have run a while loop from 1 to 10. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using \n. The loop ends when the value of i is more than 10.

Example 2

C++ program to print all the even numbers from 10 to 20 on the screen using while loop.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=10;
    while(i<=20)
    {
        cout<<i<<endl;
        i=i+2;
    }
    return 0;
}

Output

10
12
14
16
18
20

In the above example, we have run a while loop from 10 to 20. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using \n and then increment the value of i by 2. The loop ends when the value of i is more than 20.

Example 3

C++ program to print all the numbers from 10 to 1 in reverse order on the screen using while loop.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=10;
    while(i>=1)
    {
        cout<<i<<endl;
        i=i-1;
    }
    return 0;
}

Output

10
9
8
7
6
5
4
3
2
1

In the above example, we have run a while loop from 10 to 1 in reverse order. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using \n and then decrement the value of i by 1. The loop ends when the value of i is less than 1.

Example 4

C++ program to input a 4-digit number and find the sum of its digits using while loop.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int n,r,sd=0;
    cout<<"Enter a 4 digit number ";
    cin>>n;

    if(n>=1000 && n<=9999)
    {
        while(n>0)
        {
            r=n%10;     // return the last digit of the number as remainder
            sd=sd+r;
            n=n/10;     // removes the last digit of the number
        }
        cout<<"Sum of digits = "<<sd;
    }
    else
    {
        cout<<"Not a 4 digit number";
    }
    return 0;
}

Output

Enter a 4 digit number 2461
Sum of digits = 13

In the above example, we have input a 4-digit number. The loop starts with the 4-digit number and runs till the value of n is greater than 0. Each time when the loop runs, we find the last digit of the number using n%10 and add the digit in the variable sd and remove the last digit from the number using n=n/10. The loop ends when the value of n becomes 0 after removing all the digits from n.

Nested while Loop

C++ programming allows using one while loop inside another while loop. This is known as nested while loop.

Syntax of Nested while Loop

initialization_expression;
while(test_expression)
{
   // body of the outer loop
   // statements we want to execute

   initialization_expression;
   while(test expression)
   {
       // body of the inner loop
       // statements we want to execute

       update_expression;
   }
   update_expression;
}

Example

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=1,j;
    while(i<=5)
    {
        j=1;
        while(j<=i)
        {
            cout<<j;
            j++;        // it means j=j+1
        }
        cout<<"\n";
        i++;            // it means i=i+1
    }
    return 0;
}

Output

1
12
123
1234
12345

In the above example, we have run two loops, one outer loop and another inner loop. The outer loop runs from 1 to 5, and the inner loop runs from 1 to the current value of the outer loop (for example, when the value of i is 1, the inner loop runs from 1 to 1. When the value of i is 2, the inner loop runs from 1 to 2 and so on). The program ends when the outer loop test expression evaluates to false.

Infinite while Loop

An infinite while loop is a loop that never ends because its test condition never evaluates to false. It keeps on executing unless and until we terminate the loop using the break statement as shown in the example below.

Syntax of Infinite while Loop in C++

while(1)
{
    // body of the infinite while loop
}

Example

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=0;

    while(1)
    {
        cout<<"Hello World"<<endl;

        i=i+1;
        if(i==5)
        {
            break;  // terminate the infinite loop when the value of i is 5
        }
    }
    return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World
video-poster

Test Your Knowledge

Attempt the practical questions to check if the lesson is adequately clear to you.

Test Your Knowledge