The logical operators (&& and ||) are used to combine simple logical expressions into more complex ones.
&& produces the value 1 (true) if both of the operands are nonzero and produces zero (false) otherwise; therefore, if you want certain code to execute only if both test or expressions are true, use the && operator.|| produces the value 1 (true) if either of the operands are nonzero, and produces zero (false) otherwise; therefore, if you want to do something if either test is true, use the || operator.Download the file tricky-ifs.c. Compile and run the program without updating the code yet. Depending upon your compiler, you may get warnings!
Driver: The student closer to the whiteboard.
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 0$^\circ$F
Moderate danger of freezing 0$^\circ$F to -30$^\circ$F
Extreme danger of freezing Below -30$^\circ$F
Driver: The student farther to the whiteboard.
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 |
Driver: The student farther to the whiteboard.
switch statement? Why or why not?[This problem is based on an exercise by Darrah Chavey, Beloit College.]
Consider the following output for a triangle classification program:
Driver: The student closer to the whiteboard.
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;
}
Driver: The student closer to the whiteboard.
The following expression $ -2 <= x <= 2 $ is supposed to determine if a number $x$ is between $-2$ and $2$ (inclusive)
Driver: The student farther to the whiteboard.
true or false.&& operator), so that the expression correctly determines whether $x$ is between $-2$ and $2$Notes:
Relational (or comparison) operators, such as $<=$, are binary operators, so only two values are compared at a time.relational operators are combined directly, then the operator is considered to be left associative. That is, the left operator is applied and then the right one.