The While Loop
The C programming language has several structures for looping and conditional branching. We will cover them all in this chapter and we will begin with the while loop. The while loop continues to loop while some condition is true. When the condition becomes false, the looping is discontinued. It therefore does just what it says it does, the name of the loop being very descriptive.
Syntax: while ( <expression> ) <statement>
<statement> is executed repeatedly as long as the value of <expression>
remains non-zero.
The test takes place before each execution of the <statement>.
Example:
main( )
{
int count;
count = 0;
while (count < 6)
{
printf("The value of count is %d\n",count);
count = count + 1;
}
}
when this program will first check condition and then come to loop.if the loop is incorrect the it should be terminated otherwise go to loop will executed untill the condition occur .
when program executed untill the count<6 and then loop is terminated
output
The value of count is 0
The value of count is 1
The value of count is 2
The value of count is 3
The value of count is 4
The value of count is 5
output
The value of count is 0
The value of count is 1
The value of count is 2
The value of count is 3
The value of count is 4
The value of count is 5
0 comments:
Post a Comment