While Statement in C
The while statement is used to carry out looping
operations.
It is called as entry control loop since the condition is checked first before the loop is being executed.
It is called as entry control loop since the condition is checked first before the loop is being executed.
A loop is executed until the expression evaluates to
TRUE.
The syntax of while loop is as follows,
while(expression)
{
Statements;
}
The above syntax says that while the expression(i.e
, condition) is TRUE(i.e, 1) then execute the statements within the while loop
and then again it checks the condition of while statement and if true it
executes the statement and the loop keeps on going until the condition gets
FALSE(i.e, 0).
The flow chart of while loop is as follows,
Example program 1:
This program has the initial value to num as 1 and while the condition num<=5 is true the statements with in the while loop gets executed.
As you can see there is a statement num++ which is a iteration statement that increments the value of num by 1 so after the first iteration the value of num becomes 2 and again the condition is checked and it is satisfied until the fifth iteration .
After 5th iteration the value of num becomes 6 and when it checks the condition becomes FALSE since the condition is num<=5 (i.e, 6<=5 is FALSE).
Example program 2:
This program is similar to the above program but there is a slight change in the condition. here the iteration takes place only less than the 5 (that means 5 is not included).
Output:
Example Program 3:
This program has a infinite loop as we can see the value of the num remains 1 through out program so in the terminal the number 1 keeps on printing until we stop the program.
Output:
Thank you …
Please subscribe below to receive the post on mail...
0 comments: