Tutorial Study Image

While ,do while and Infinite Loop


August 19, 2022, Learn eTutorial
1565

In the previous tutorials, we learned about the basic concept of the loop in C++ and For Loop in detail.

When studying the for loop, we discovered that the number of iterations is known in advance, i.e., how many times the body of the loop must be executed is known to us. While loops are used when we don't know the exact number of loop iterations ahead of time. The execution of the loop is terminated based on the test conditions.

In C++  programing language, the while loop is another major looping construct. The while loop takes a logical expression and executes the loop based on the validation of the logical expression. If the expression or condition is true the block of codes inside it will get executed else in other cases will not execute.

The while is also a reserved word to denote the basic control flow structure.

Syntax of While Loop

As previously stated, a loop is made up of three statements: an initialization expression, a test expression, and an update expression. The syntax of the three loops - For, while, and do while – mainly differs in the order in which these three statements are placed.
 


initialization expression;
while (test_expression)
{
   // statements
 
  update_expression;
}

 
  • while loop evaluates the test_expression
  • If the test_expression evaluates to true, the code inside the while loop is executed.
  • The test_expression is evaluated again.
  • This process continues until the test_expression is false.
  • When the test_expression evaluates to false, the loop terminates.

Example


#while loop

int i=1
while(i<=10){
  cout << i;
  i++;
}

The output generated is a sequence of numbers from 1 to 10. The execution terminates when the iterator variable i become 10. The condition fails to satisfy if i is incremented further. When i=11 (i=10+1) checks the condition (i<=10) where 11 is not less than 10 .Here condition becomes FALSE and exits from the loop.


[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

Flow Chart of While loop

C++ : While Loop

C++ program examples to demonstrate the while loop

Example 1: Display a text 5 times


#include <iostream>
using namespace std;

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

    // test expression
    while (i < 6)
    {
        cout << "learn eTutorials\n";

        // update expression
        i++;
    }

    return 0;
}

Output:

Learn eTutorials
Learn eTutorials
Learn eTutorials
Learn eTutorials
Learn eTutorials

We can check how this program works

Iteration Variable i <= 5 Action
1st i = 1 true Learn eTutorials is printed and i is increased to 2.
2nd i = 2 true Learn eTutorials is printed and i is increased to 3.
3rd i = 3 true Learn eTutorials is printed and i is increased to 4.
4th i = 4 true Learn eTutorials is printed and i is increased to 5.
5th i = 5 true Learn eTutorials is printed and i is increased to 6.
6th i = 6 false The loop is terminated

do .. while Loop

In do-while loops, the loop execution is also terminated based on test conditions. The main difference between a do-while loop and a while loop is that the condition in the do-while loop is tested at the end of the loop body, whereas the other two loops are entry-controlled loops.

The loop body will run at least once in a do-while loop regardless of the test condition.

Syntax of do .. while Loop


initialization expression;
do
{
   // statements

   update_expression;
} while (test_expression);
 

Flow Chart of do .. while loop

C++ : do .. while Loop

C++ program to demonstrate the do while loop

Example 1: Display a text 


// C++ program to illustrate do-while loop
#include <iostream>
using namespace std;

int main()
{
    int i = 2; // Initialization expression

    do
    {
        // loop body
        cout << "learn eTutorials\n";

        // update expression
        i++;

    } while (i < 1); // test expression

    return 0;
}

Output:

Learn eTutorials

The test condition (i<1) evaluates to false in the program mentioned above. However, since the loop is exit-controlled, its body will only run once.

What about the concept of an Infinite Loop?

An infinite loop (also known as an endless loop) is a piece of code that has no functional exit and thus repeats indefinitely. When a condition is always evaluated as true, an infinite loop occurs. This is usually an error.

C++ program to demonstrate infinite loops using for and while


#include <iostream>
using namespace std;
int main ()
{
    int i;

    // This is an infinite for loop as the condition
    // expression is blank
    for ( ; ; )
    {
        cout << "Learn etutorials.\n";
    }

    // This is an infinite for loop as the condition
    // given in while loop will keep repeating infinitely
    /*
    while (i != 0)
    {
        i-- ;
        cout << "Learn etutorials.\n";
    }
    */

    // This is an infinite for loop as the condition
    // given in while loop is "true"
    /*
    while (true)
    {
        cout << "Learn etutorials.\n";
    }
    */
}

Output:

learn eTutorials.
learn eTutorials.
learn eTutorials.
learn eTutorials.
learn eTutorials.
……………………………………………….
………………………………………………….

Example 2: C++ program to demonstrate infinite loops using while Loop


#include <iostream>
using namespace std;

int main()
{

    while (1)
        cout << "learn eTutorials.\n";
    return 0;
}

Output:

learn eTutorials.
learn eTutorials.
learn eTutorials.
learn eTutorials.
learn eTutorials.
……………………………………………….
………………………………………………….

Example 3: C++ program to demonstrate infinite loops using do while Loop


#include <iostream>
using namespace std;

int main() {

    do{
        cout << " Learn etutorials.\n";
    } while(1);

    return 0;
}

Output:

learn eTutorials.
learn eTutorials.
learn eTutorials.
learn eTutorials.
learn eTutorials.
……………………………………………….
………………………………………………….