While-Loop in C++

While-loop

A while-loop in computer science is used when  there no idea on the number of times the loop body needs to be executed beforehand

Definition: In this type of loops, the test condition is tested before entering the loop body. such type of  loops are called entry controlled loop

ex:  while, for loops

C++ program to print even numbers from 1 to 20

main()
{

int i=0;//initilisation

while(i<=20)//test condition
{
if(i%2==0)
cout<<i<<"\t";
i++;//update expression
}

cout<<"control flow reached here";

}

O/P

2 4 6 8 10 12 14 16 18 20
control flow reached here
  • In the above example condition is true for the first 20 times and becomes false for i==21 and then loop  breaks
  • Hence the loop body is executed 20 times and the condition is checked 21 times i.e 1 time  to becomes false at the end

Flow of Execution

  1. In while loop, the condition is checked first it
  2. If it is true then the statements inside while loop executes 
  3. This is done repeatedly until the condition becomes false.
  4. When condition returns false, the control comes out of loop and
  5. Jumps to the next statement in the program after while loop

C++ program to find the sum of even digits in a number

main()
{

int n,digit,sum=0;

cout<<"\nenter a number:";
cin>>n;

while(n!=0)
{
digit = n % 10;//returns individual digits from right
n = n / 10;//to retrieve next digit
if(digit % 2== 0)//check if that individual digits is even are not
sum=sum+digit;
}

cout<<"\nsum is"<<sum;

}

Output

enter a sum
1234
sum is 6

1357
sum is 0

Designing an Infinite loop in while

  • As the name suggests, The condition part in the loop statement is always true and never becomes false
  • Hence the loop will not stop to execute and control never returns out of the loop

Variant 1: Missing the increment/decrement operators

main()
{
int i=0;
while(i<10)
{
cout<<i;//no ++/-- part
}
}

O/P

0 0....infinite times

In the above example, 0 is always less than 10 hence always true is returned

Variant 2: Explicitly writing the true value

main()
{
while(true)//no chance that it becomes false
{
cout<<"trishaank";
}
}

O/P

trishaank .....infinite times

No statement in the loop body makes it false. hence it executes indefinitely

Variant 3: Inappropriate use of zero and relational operators

main()
{
int i=1;
while(i>0)
{
cout<<i<<"\t";
i++;
}
}

O/P

1 2 3 4 5 ........infinity

‘i’ will go on incrementing, because 0 is always greater than any number

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
main()
{
int i=1;
while(i<=10);//semicolon is stopping not to go down
{
cout<<i<<"\t";//loop body is not executed
i++;
}
cout<<"This is will not 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