The general form of while is as shown below:
initialise loop counter ;
while ( test loop counter using a condition )
{
do this ;
and this ;
increment loop counter ;
}
Note the following points about while...
− The statements within the while loop would keep on getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop.
In place of the condition there can be any other valid expression. So long as the expression evaluates to a non-zero value the statements within the loop would get executed.
− The condition being tested may use relational or logical operators as shown in the following examples:
while ( i <= 10 )
while ( i >= 10 && j <= 15 )
while ( j > 10 && ( b < 15 || c < 20 ) )
− The statements within the loop may be a single line or a block of statements. In the first case the parentheses are optional. For example,
while ( i <= 10 )
i = i + 1 ;
is same as
while ( i <= 10 )
{
i = i + 1 ;
}
− As a rule the while must test a condition that will eventually become false, otherwise the loop would be executed forever, indefinitely.
main( )
{
int i = 1 ;
while ( i <= 10 )
printf ( "%d\n", i ) ;
}
initialise loop counter ;
while ( test loop counter using a condition )
{
do this ;
and this ;
increment loop counter ;
}
Note the following points about while...
− The statements within the while loop would keep on getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop.
In place of the condition there can be any other valid expression. So long as the expression evaluates to a non-zero value the statements within the loop would get executed.
− The condition being tested may use relational or logical operators as shown in the following examples:
while ( i <= 10 )
while ( i >= 10 && j <= 15 )
while ( j > 10 && ( b < 15 || c < 20 ) )
− The statements within the loop may be a single line or a block of statements. In the first case the parentheses are optional. For example,
while ( i <= 10 )
i = i + 1 ;
is same as
while ( i <= 10 )
{
i = i + 1 ;
}
− As a rule the while must test a condition that will eventually become false, otherwise the loop would be executed forever, indefinitely.
main( )
{
int i = 1 ;
while ( i <= 10 )
printf ( "%d\n", i ) ;
}
This is an indefinite loop, since i remains equal to 1 forever. The correct form would be as under:
main( )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
}
− Instead of incrementing a loop counter, we can even decrement it and still manage to get the body of the loop executed
repeatedly. This is shown below:
main( )
{
int i = 5 ;
while ( i >= 1 )
{
printf ( "\nMake the computer literate!" ) ;
i = i - 1 ;
}
}
− It is not necessary that a loop counter must only be an int. It can even be a float.
main( )
{
float a = 10.0 ;
while ( a <= 10.5 )
{
printf ( "\nRaindrops on roses..." ) ;
printf ( "...and whiskers on kittens" ) ;
a = a + 0.1 ;
main( )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
}
− Instead of incrementing a loop counter, we can even decrement it and still manage to get the body of the loop executed
repeatedly. This is shown below:
main( )
{
int i = 5 ;
while ( i >= 1 )
{
printf ( "\nMake the computer literate!" ) ;
i = i - 1 ;
}
}
− It is not necessary that a loop counter must only be an int. It can even be a float.
main( )
{
float a = 10.0 ;
while ( a <= 10.5 )
{
printf ( "\nRaindrops on roses..." ) ;
printf ( "...and whiskers on kittens" ) ;
a = a + 0.1 ;
}
}
− Even floating point loop counters can be decremented. Once again the increment and decrement could be by any value, not necessarily
}
− Even floating point loop counters can be decremented. Once again the increment and decrement could be by any value, not necessarily
What do you think would be the output of the following
program?
main( )
{
int i = 1 ;
while ( i <= 32767 )
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
}
No, it doesn’t print numbers from 1 to 32767. It’s an indefinite loop. To begin with, it prints out numbers from 1 to 32767. After that value of i is incremented by 1, therefore it tries to become 32768, which falls outside the valid integer range, so it goes to other side and becomes -32768 which would certainly satisfy the condition in the while. This process goes on indefinitely.
− What will be the output of the following program?
main( )
{
int i = 1 ;
while ( i <= 10 ) ;
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
}
This is another indefinite loop, and it doesn’t give any output at all. The reason is, we have carelessly given a ; after the while. This would make the loop work like this...
while ( i <= 10 )
;
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
Since the value of i is not getting incremented the control would keep rotating within the loop, eternally. Note that enclosing printf( ) and i = i +1 within a pair of braces is not an error. In fact we can put a pair of braces around any individual statement or set of statements without affecting the execution of the program.
while ( i <= 10 )
;
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
Since the value of i is not getting incremented the control would keep rotating within the loop, eternally. Note that enclosing printf( ) and i = i +1 within a pair of braces is not an error. In fact we can put a pair of braces around any individual statement or set of statements without affecting the execution of the program.
No comments:
Post a Comment