Skip to main content

Posts

Showing posts from September 21, 2008

The While Loop

The While Loop While programming we often need to do something a fixed number of times. In such cases the While loop is used. Lets study it with the help of an example below; /* Calculation of simple interest for 3 sets of p, n and r */ main( ) { int p, n, count ; float r, si ; count = 1 ; while ( count <= 3 ) { printf ( "\nEnter values of p, n and r " ) ; scanf ( "%d %d %f", &p, &n, &r ) ; si = p * n * r / 100 ; printf ( "Simple interest = Rs. %f", si ) ; count = count + 1 ; } } The program above executes all statements after while three times.The logic of calculating simple interest is written in braces after the while word and is called as the body of the while loop.The brackets after the while contains the condition.All the statements within the loop are executed till the condition is true. Another program which uses while loop,to print the average of the numbers input . The Program