While-Loop in C++

While Loop

Here, in this section, we will discuss the While loop in C++.

Definition: In this type of loop, the test condition is tested before entering the loop body. Such types of loops are called Entry Controlled Loops.

While loop in C++

Looping in C++

C++ has the following loops to control the program execution –

We will be discussing while loop on this post.

While Loop Syntax

while(condition(s))
{
    // execute statement(s)

}
  • Certain condition(s) are checked before the loop
  • If the condition(s) is(are) true, code inside is executed
  • The condition then is evaluated again. If true then the code inside is executed again.
  • This will keep happening until the condition doesn’t become false.
While Loop in C++

Program 0 (Print Hello World N times)

#include<iostream>
using namespace std;
 
int main()
{
    int num = 5;
    
    int i = 1;
    while(i <= num)
    {
        cout << "Hello World\n";
        i++;
    }
    return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World

Program 1 (Print first 10 Natural Numbers)

#include<iostream>
using namespace std;
 
int main()
{
    int i = 1; // initialization
    
    // condition
    while(i <= 10)
    {
        cout << i << " ";;
        i++; // increment
    }
    
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10 

Program 2 (Sum of first N Natural Numbers)

#include<iostream>
using namespace std;
 
int main()
{
    int num = 20, sum = 0;
    
    int i = 1;
    while(i <= num)
    {
        sum = sum + i;
        i++;
    }
    
    cout << "Sum of first " << num << " Natural numbers: " << sum;
    
    return 0;
}

Output

Sum of first 20 Natural numbers: 210

Infinite while loop

A while loop can run infinitely if the conditions are not checked properly. Following are examples of while loops running definitively –

  • Explicitly writing true in condition
  • Setting conditions that they will always be true
  • Forgetting to increment the iterator of the loop

Example 1

// condition will always be true infinitively
while(true)
{
    // do some work here
}

Example 2

int i = 100;
    
// condition will always be true infinitively
while(i > 10)
{
    cout << i << " ";
    i++;
}
    
// will keep on printing numbers from 100 to infinity

Example 3

int i = 1;
    
// condition will always be true infinitively
while(i < 10)
{
    cout << i << " ";
    // forgot i++ here
}
    
// will keep on printing 1 infinitively

Inappropriate use of the semicolon

  • A semicolon after the condition part will stop the next statements not to  execute
  • Hence all the statements after the semicolon are not executed and just dead statements
#include<iostream>
using namespace std;
 
int main()
{
    int i=1;
    while(i <= 10);//semicolon is stopping not to go down
    {
        court << i << "\t"; //loop body is never  executed
        i++;
   }
   cout << "This is will never print";
}

If you execute this code you will get an empty screen as output because  after semicolon the increment  statements and two cout statements are not allowed to execute i.e after semicolon whatever it may be nothing is executed in case of loops