Our social:

Monday, October 3, 2016

C++ While Loop

While loop can be presented in the following way

while (expression) statement
OR

while (expression) 
{
 statement
}
Expression: Expressions are sequences of operators and operands. For example 32 + 5,a + b + cx + y * 5 / zatruefalse0x < 10, etc are expressions.
The output of the expression will be a boolean (true or false). If expression returns true then we will enter the while loop else we will exit the while loop.
Note: All non-zero values will be converted to true, and zero values to false. Negative numbers are non-zero so they will be converted to true.
In above syntax "statement" is a line of instructions or a block of instructions. As the value of an expression is true the statement will get executed. If the value of an expression is false the while loop will be terminated. Take a look on various examples of while loops:

Infinite loop


while(true)
{
 cout << "I am inside loop" << endl;
}
Above loop will run forever because the expression true is always true. Below code is also similar to above code since 1 is equal to true.

while(1)
{
 cout << "I am inside loop" << endl;
}
Output of above code will be
I am inside loop
I am inside loop
I am inside loop
   …
   …
   …
will run forever

how to break an infinite loop?

We can break an infinite loop using control statements like break and goto.

while(1)
{
 cout << "I am inside loop" << endl;
 if(i==1)
 {
  break;
 }
}
In above code "i" is a varible which is getting change by other code snippet.

Finite loop

For finite loops we should have an expression which should give a false value some how. In below code the expression will give false value if value of x will be greater than or equal to 10. Since we are incrementing value of x so after some time condition x < 10 will return false and while loop will terminate.

int x = 0;
while (x < 10) // the condition is "x < 10"
{
 ++x; // statement executed in loop
}
cout << "Now x is " << x << endl;
Output will be "Now x is 10"
To perform specific operation multiple time we put that operation inside the loop. In below code we want to print value of x multiple time till x < 20

int x = 10;
while (x < 20)// the condition is "x < 20"
{
 // this block is executed in loop
 ++x;
 cout  << "Now x is " << x << endl;
}
The output of above code will be
Now x is 11
Now x is 12
Now x is 13
Now x is 14
Now x is 15
Now x is 16
Now x is 17
Now x is 18
Now x is 19
Now x is 20
In below code the value of x is decrementing and as value of x will reach zero then while loop will terminate.

int x = 10;
while (x)// the condition while x. It means while x is not equal to zero
 --x;
cout << "Now x is " << x << endl;
Above program gives the output
Now x is 0
The sequence of actions in the while loop is the following:
Step1: Check condition. If condition is true go to Step2. Else Stop.
Step2: Perform block of instructions. Go to Step1
Below is the flow diagram of a while loop.

While Loop

        

  C++ Do While Loop

0 comments:

Post a Comment

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you