September 1998
Intermediate to advanced
848 pages
20h 13m
English
The simple form of an if statement gives you the choice of executing a statement (possibly compound) or skipping it. C also enables you to choose between two statements by using the if else form. Let's use the if else form to fix an awkward segment from Listing 7.1.
if (all_days != 0)
printf("%d days total: %.1f%% were below freezing.\n",
all_days, 100.0 * (float) cold_days / all_days);
if (all_days == 0)
printf("No data entered!\n");
If the program finds that days is not equal to 0, it should know that days must be 0 without retesting, and it does. With if else you can take advantage of that knowledge by rewriting the fragment this way:
if (all_days!= 0) printf("%d days total: %.1f%% were below freezing.\n", ...Read now
Unlock full access