Introducing Loops

I l @ ve RuBoard

Introducing Loops

Listing 5.1 shows a sample program that does a little arithmetic to calculate the length in inches of a foot that wears a size 9 (men's) shoe. This first version doesn't use loops.

Listing 5.1 The shoes1.c program.
 /* shoes1.c -- converts a shoe size to inches */ #include <stdio.h> #define ADJUST 7.64 #define SCALE 0.325 int main(void) {   double shoe, foot;   shoe = 9.0;   foot = SCALE * shoe + ADJUST;   printf("Shoe size (men's)    foot length\n");   printf("%10.1f %15.2f inches\n", shoe, foot);   return 0; } 

Here is a program with multiplication and addition. It takes your shoe size (if you wear a size 9) and tells you how long your foot is in inches. "But," you say, "I could solve this problem by hand more quickly than you could type the program." That's a good point. A one-shot program that does just one shoe size is a waste of time and effort. You could make the program more useful by writing it as an interactive program, but that still barely taps the potential of a computer.

What you need is some way to have a computer do repetitive calculations for a succession of shoe sizes. After all, that's one of the main reasons for using a computer to do arithmetic. C offers several methods for doing repetitive calculations, and we will outline one here. This method, called a while loop, will enable you to make a more interesting exploration of operators. Listing 5.2 presents the improved shoe-sizing program.

Listing 5.2 The shoe2.c program.
 /* shoe2.c -- calculates foot lengths for several sizes */ #include <stdio.h> #define ADJUST 7.64 #define SCALE 0.325 int main(void) {   double shoe, foot;   printf("Shoe size (men's)    foot length\n");   shoe = 3.0;   while (shoe < 18.5)         /* starting the while loop */   {                           /* start of block          */      foot = SCALE*shoe + ADJUST;      printf("%10.1f %15.2f inches\n", shoe, foot);      shoe = shoe + 1.0;   }                           /* end of block            */   printf("If the shoe fits, wear it.\n");   return 0; } 

Here is a condensed version of shoe2.c's output:

 Shoe size (men's)    foot length        3.0            8.62 inches        4.0            8.94 inches        ...            ...       17.0           13.16 inches       18.0           13.49 inches If the shoe fits, wear it. 

(Incidentally, the constants for this conversion were obtained during an incognito visit to a shoe store. The only shoe- sizer left lying around was for men's sizes. Those of you interested in women's sizes will have to make your own visit to a shoe store.)

Here is how the while loop works. When the program first reaches the while statement, it checks to see whether the condition within parentheses is true. In this case, the expression is as follows :

 shoe < 18.5 

Here, the < symbol means "is less than." The variable shoe was initialized to 3.0 , which certainly is less than 18.5 . Therefore, the condition is true and the program proceeds to the next statement, which converts the size to inches. Then it prints the results. The next statement increases shoe by 1.0, making it 4.0:

 shoe = shoe + 1.0; 

At this point, the program returns to the while portion to check the condition. Why at this point? Because the next line is a closing brace (}), and you have used a set of braces ({ }) to mark the extent of the while loop. The statements between the two braces are the ones that are repeated. The section of program between and including the braces is called a block . Now back to your program. The value 4 is less than 18.5 , so the whole cycle of embraced commands (the block) following the while is repeated. (In computerese, the program is said to "loop" through these statements.) This continues until shoe reaches a value of 19.0 . Now the condition

 shoe < 18.5 

becomes false because 19.0 is not less than 18.5 . When this happens, control passes to the first statement following the while loop. In this case, that is the final printf() statement.

You can easily modify this program to do other conversions. For example, change SCALE to 1.8 and ADJUST to 32.0 , and you have a program that converts Centigrade to Fahrenheit. Change SCALE to 0.6214 and ADJUST to , and you convert kilometers to miles. If you make these changes, you should change the printed messages, too, to prevent confusion.

The while loop provides a convenient , flexible means of controlling a program. Now let's turn to the fundamental operators that you can use in your programs.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net