To combine two tests, use the && (AND), and || (OR) operators. If you want to be certain code to execute only if both tests are true, use the AND operator. If your want to do something if either test is true use the OR operator.
if Statment and ComparisonsFor this problem, use the code in the program tricky-ifs.c. It should compile and run without obvious problem, but the results may not be what you expect. Depending upon your compiler, you may get warnings that will give you hints!
Windchill temperatures Twc (in Fahrenheit) may be approximated* from the temperature F in Fahrenheit) and the wind speed W (in miles per hour) according to the formula
Twc = F - (W * 0.7)
Windchill temperatures are rated in the following categories (for covered skin):
| Little danger of freezing | above 0oF |
| Moderate danger of freezing | 0oF to -30oF |
| Extreme danger of freezing | Below -30oF |
Using this starter code, windchill.c, write a program that reads the current Fahrenheit temperature and wind speed and prints both the windchill value and the corresponding windchill category. You may assume that the user will enter a valid number. (We will soon learn how to read values from the keyboard and test that they are valid. In the meantime, follow this example for numeric input.)
The U.S. Weather Service describes winds of various strengths according to the following table:
| Velocity (MPH) | Term |
|---|---|
| Less than 1 | Calm |
| 1–7 | Light |
| 8–12 | Gentle |
| 13–18 | Moderate |
| 19–24 | Fresh |
| 25–38 | Strong |
| 39–54 | Gale |
| 55–75 | Whole Gale |
| Above 75 | Hurricane |
Write a program that reads the speed of the wind (in MPH) and then prints the corresponding descriptive term.
Can you write this program using a switch statement? Why or why not?
Write a C program that reads the lengths of three sides of a triangle and prints out the classification of the triangle as follows:
In this problem, you should assume that the input values are all numbers, although error checking may indicate some numbers are negative or zero.
Often, expressions involving && and || operators are quite useful in formulating conditions, but sometimes conditions can be simplified considerably. For example, consider the following program, which classifies an integer as being negative, zero, small (a single digit), medium (between 10 and 20), and large (bigger than 20).
/* program to classify integers into simple categories */
#include <stdio.h>
int main () {
/* getting started by reading a number*/
printf ("program to classify integers\n");
int number;
printf ("Enter your integer candidate: ");
scanf ("%d", &number);
/* classify number */
if (number < 0)
printf ("%d is negative\n", number);
else if (number == 0)
printf ("number is zero\n");
else if ((number >= 0) && (number < 10))
printf ("%d is a single digit\n", number);
else if ((number >= 10) && (number <= 20))
printf ("%d has medium size\n", number);
else if (number > 20)
printf ("%d is large\n", number);
else
printf ("there is no need for this statement --- why?\n");
return 0;
}
Rewrite this program, simplifying the conditions by use the results of one test to infer the results of later tests.
The following expression is supposed to determine if a number x is between -2 and 2 (inclusive)
-2 <= x <= 2
-2 <= x <= 2is evaluated as
((-2 <= x) <= 2)
* The simplified calculation comes from Tom Skilling at the Chicago Tribune.