Conditionals Lab

Testing Two or More Things in a Single Expression: The && and || Operators

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.

Problem 1: The if Statment and Comparisons

For 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!

  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 a. (This is at about line 13.)
  3. Change the original assignment TEST = 1 to TEST = -23 and repeat Part a.
  4. Change the original assignment TEST = 1 to TEST = 0 and repeat Part a. Why does "statement 13" print?

Problem 2: 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 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.)

Problem: 3 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

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?

Problem 4: Classifying Triangles

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

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.

Problem 5: 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; 
}  

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

Problem 6: Complicated Comparisons

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

    -2 <= x <= 2   
  1. Include this expression in a program, and run the program for various values of x, such as -3, -2, -1, -, 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 &&), so that the expression correctly determines whether x is between -2 and 2

Notes

Acknowledgements

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