Home | | Computer Science 11th std | C++ While loop

Chapter: 11th Computer Science : Chapter 10 : Flow of Control

C++ While loop

A while loop is a control flow statement that allows the loop statements to be executed as long as the condition is true.

While loop

 

A while loop is a control flow statement that allows the loop statements to be executed as long as the condition is true. The while loop is an entry-controlled loop because the test-expression is evaluated before the entering into a loop.

The while loop syntax is:

while ( Test expression )

{

      Body of the loop;

}

Statement-x;

The control flow and flow chart of the while loop is shown below.


Flowchart 10.7: while loop control flow and while loop flowchart

In while loop, the test expression is evaluated and if the test expression result is true, then the body of the loop is executed and again the control is transferred to the while loop. When the test expression result is false the control is transferred to statement-x.

 

Illustration 10.14 C++ program to sum numbers from 1 to 10 using while loop

#include <iostream>

using namespace std;

int main ()

{

int i=1,sum=0;

while(i<=10)

{

      sum=sum+i;

      i++;

}

cout<<"The sum of 1 to 10 is "<<sum;

return 0;

}

Output

The sum of 1 to 10 is 55

 

In the above program, the integer variable i is initialized to 1 and the variable sum to 0. The while loop checks the condition, i < 10, if the condition is true, the value of i, which is added to sum and i is incremented by 1. Again, the condition i < 10 is checked. Since 2 < 10, 2 is added to the earlier value of sum. This continues until i becomes 11. At this point in time, 11< 10 evaluates to false and the while loop terminates. After the loop termination, the value of sum is displayed.

 

Illustration 10.15 C++ program to sum numbers from 1 to 10 using while loop

#include <iostream>

using namespace std;

int main ()

{

int i=1,num,avg,sum=0;

while(i<=5)

{

      cout<<"Enter the number : ";

      cin>>num;

      sum=sum+num;

      i++;

}

avg=sum/5;

cout<<"The sum is "<<sum<<endl;

cout<<"The average is "<<avg;

return 0;

}

Output

Enter the number : 1

Enter the number : 2

Enter the number : 3

Enter the number : 4

Enter the number : 5

The sum is 15

The average is 3

In the above program, integer variables num and avg are declared and variable i is initialized to 1 and sum to 0. The while loop checks the condition, since i <= 5 the condition is true, a number is read from the user and this is added to sum and i is incremented by 1. Now, the condition is i <= 5 is again checked. Since 2 <=5, the second number is obtained from the user and it is added to sum. This continues, until i becomes 6, at which point the while loop terminates. After the loop termination, the avg is computed and both sum and avg are displayed.

 

While loop variation

A while loop may contain several variations. It can be an empty loop or an infinite loop. An empty while loop does not have any statement inside the body of the loop except null statement i.e., just a semicolon.

For example


In the above code, the loop is a time delay loop. A time delay loop is useful for pausing the program for some time.

A while loop may be infinite loop when no update statement inside the body of the loop. For example,


Similarly, there is another variation of while is also shown below:

int main()

{

      int i=1;

      while( ++i < 10)

      cout<< “The value of i is “<<i;

      return 0;

}

In the above statement while ( ++i < 10 ), first increment the value of i, then the value of i is compared with 10.

int main()

{

      int i=1;

      while( i++ < 10)

      cout<< “The value of i is “<<i;

      return 0;

}

In the above statement while ( i++ < 10 ), first the value of i is compared with 10 and then the incrementation of i takes place. When the control reaches cout<< “The value of i is “<<i statement, i has already been incremented.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 10 : Flow of Control : C++ While loop |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.