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.
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_expressiontrue
, the code inside the while
loop is executed.false
.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
#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 |
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.
initialization expression;
do
{
// statements
update_expression;
} while (test_expression);
// 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.
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.
#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. ………………………………………………. ………………………………………………….
#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. ………………………………………………. ………………………………………………….
#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. ………………………………………………. ………………………………………………….