Monday, July 15, 2013

Loops

The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive o
throug
program. They are: (a) Using a for statement (b) Using a while statement (c) Using a do-while statement
Each of these methods is discussed in the following pages.

The While loop

It is often the case in programming that you want to do something
a fixed number of times. Perhaps you want to calculate gross
salaries of ten different persons, or you want to convert
temperatures from centigrade to fahrenheit for 15 different cities.
The while loop is isimple example, w
deally suited for such cases.
/* 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 " ) ;
d %f", &p, &n, &r ) ;
n * r / 100 ;
printf ( "Simple interest = Rs. %f", si ) ;
count=count+1;
}}


The general form of while is as shown below:
initialise loop counter ;
increment loop counter ;
o about while...





No comments:

Post a Comment