There are variety of operators which are frequently used with while. To illustrate their usage let us consider a problem wherein numbers from 1 to 10 are to be printed on the screen. The program for performing this task can be written using while in the following different ways:
(a) main( )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
}
(a) main( )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
}
(b) main( )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i++ ;
}
}
Note that the increment operator ++ increments the value of i by 1, every time the statement i++ gets executed. Similarly, to reduce the value of a variable by 1 a decrement operator -- is also available.
However, never use n+++ to increment the value of n by 2, since C doesn’t recognize the operator +++.
(c) main( )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i += 1 ;
}
}
Note that += is a compound assignment operator. It increments the value of i by 1. Similarly, j = j + 10 can also be written as j += 10. Other compound assignment operators are -=, *=, / = and %=.
(d) main( )
{
int i = 0 ;
while ( i++ < 10 )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i++ ;
}
}
Note that the increment operator ++ increments the value of i by 1, every time the statement i++ gets executed. Similarly, to reduce the value of a variable by 1 a decrement operator -- is also available.
However, never use n+++ to increment the value of n by 2, since C doesn’t recognize the operator +++.
(c) main( )
{
int i = 1 ;
while ( i <= 10 )
{
printf ( "%d\n", i ) ;
i += 1 ;
}
}
Note that += is a compound assignment operator. It increments the value of i by 1. Similarly, j = j + 10 can also be written as j += 10. Other compound assignment operators are -=, *=, / = and %=.
(d) main( )
{
int i = 0 ;
while ( i++ < 10 )
printf ( "%d\n", i ) ;
}
In the statement while ( i++ < 10 ), firstly the comparison of value of i with 10 is performed, and then the incrementation of i takes place. Since the incrementation of i happens after its usage, here the ++ operator is called a post-incrementation operator. When the control reaches printf ( ), i has already been incremented, hence i must be initialized to 0.
(e) main( )
{
int i = 0 ;
while ( ++i <= 10 )
printf ( "%d\n", i ) ;
}
In th statement e while ( ++i <= 10 ), firstly incrementation of i takes place, then the comparison of value of i with 10 is performed. Since the incrementation of i happens before its usage, here the ++ operator is called a pre- incrementation operator
}
In the statement while ( i++ < 10 ), firstly the comparison of value of i with 10 is performed, and then the incrementation of i takes place. Since the incrementation of i happens after its usage, here the ++ operator is called a post-incrementation operator. When the control reaches printf ( ), i has already been incremented, hence i must be initialized to 0.
(e) main( )
{
int i = 0 ;
while ( ++i <= 10 )
printf ( "%d\n", i ) ;
}
In th statement e while ( ++i <= 10 ), firstly incrementation of i takes place, then the comparison of value of i with 10 is performed. Since the incrementation of i happens before its usage, here the ++ operator is called a pre- incrementation operator
No comments:
Post a Comment