Lab: Booleans and Conditionals

The logical operators (&& and ||) are used to combine simple logical expressions into more complex ones.

  • The “and” operator && 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.
  • The “or” 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.

A. The if Statement and Comparisons

Download the file tricky-ifs.c. Compile and run the program without updating the code yet. Depending upon your compiler, you may get warnings!

Exercises

Driver: The student closer to the whiteboard.

  1. Compile and run the program. Explain why you get the output that results, especially the fact that it will print “statement 7” as part of the results.
  2. Change the original assignment TEST = 1 to TEST = 7 and repeat Part 1. (This is at about line 13.)
  3. Change the original assignment TEST = 1 to TEST = -23 and repeat Part 1.
  4. Change the original assignment TEST = 1 to TEST = 0 and repeat Part 1. Why does “statement 13” print?

B. Calculating Windchill

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

Exercises

Driver: The student farther to the whiteboard.

  1. Using this starter code, windchill.c, complete 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.)

C. Wind Speed Classifications

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

Exercises

Driver: The student farther to the whiteboard.

  1. Write a program that reads the speed of the wind (in MPH) and then prints the corresponding descriptive term.
  2. Can you write this program using a switch statement? Why or why not?

D. Classifying Triangles

[This problem is based on an exercise by Darrah Chavey, Beloit College.]

Consider the following output for a triangle classification program:

invalid input
if any sides are zero or negative in length
not a triangle
the sum of any two sides must be greater than the length of the third so give this message if that is not true
scalene triangle
no two sides have equal length
isosceles triangle
two sides (but not the third) are equal
equilateral triangle
all three sides are equal

Exercises

Driver: The student closer to the whiteboard.

  1. Write a C program that reads the lengths of three sides of a triangle and prints out the classification of the triangle as described above. In this problem, you should assume that the input values are all numbers, although error checking may indicate some numbers are negative or zero.

E. Simplfying Multiple Condition Checking

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; 
}  

Exercises

Driver: The student closer to the whiteboard.

  1. Rewrite this program, simplifying the conditions by using the results of one test to infer the results of later tests.

F. Complicated Comparisons

The following expression $ -2 <= x <= 2 $ is supposed to determine if a number $x$ is between $-2$ and $2$ (inclusive)

Exercises

Driver: The student farther to the whiteboard.

  1. Include this expression in a program (as it is written), and run the program for various values of $x$, such as $-3, -2, -1, 0, 1, 2, 3$. In each case, indicate whether the expression is true or false.
  2. Explain why the results are obtained. (You may get a compiler warning. Pay attention to what it says!)
  3. Rewrite the expression (using the && 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.
  • When 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.
  • Thus, $-2 <= x <= 2 $ is evaluated as $((-2 <= x) <= 2)$. Typically, when a comparison is made, the result is considered as 0, if the comparison is false, or 1, if the comparison is true.

Acknowledgements

  • The simplified calculation comes from Tom Skilling at the Chicago Tribune.