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. To enhance your appreciation of loops, this first version illustrates the limitations of programming without using a loop.
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. ...
Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.