CSCI 201 Study Aid: Conditional Structures

Conditional Structures

Simple if statement

Examples:

The if-else statement

Multiple possibilities

Switch Statements

The switch statement is very similar to the if-else-if structure of selection statements. Here is its general form:

   switch(expression) 
   { 
      case constant_1: 
	 statement_1 or block1;
	 break; 
      case constant_2: 
	 statement_2 or block_2; 
	 break;
      case constant_3:
	 statement_3 or block_3; 
	 break; 

// More cases may follow 

      default: 
	 default_statement;
	 break;
} 

The switch statement evaluates the value of an expression which must evaluate to an integer or to a character. Execution then passes to the case whose label matches the value of the expression. Duplicate labels are not allowed, so only one case will be selected. If no matching label is found, the execution passes to the default label. If there is no default label (and no matching case label), the statement does nothing and execution passes to next_statement.

Each case is initiated by the appearance of the case reserved word, and is usually terminated by the appearance of the word break. The break is not required.

There is one subtle but very important difference between the if-else-if structure and the switch statement. That difference involves the break statement. In the if-else-if structure, no matter what, only one of the blocks of code is executed. The break statement inside each of the case blocks has the same effect - it tells the computer to continue the execution after the switch statement (execution passes to the next_statement). If the break statement is omitted, the flow of execution will go forward into the next block. Leaving out a break is sometimes useful, but pretty rare. If you are missing a break statement in any block, make sure you intended to leave it out!

The if-else-if and the switch structures

The if-else-if and the switch structures have very similar usage in a program, but there are distinct differences.

Example 1

#include <iostream.h> int main() { float a, b; char operation; cout << "Enter a number, an operation, and a number:"; cin >> a >> operation >> b; cout << endl << endl << "The answer, Master, is: "; switch (operation) { case '+': cout << a + b; break; // Without this statement, execution // continues on to case '-' case '-': cout << a - b; break; case 'x': // Users may use either symbol for // multiplication. case '*': // Notice that for either case, the next // block will be executed. cout << a * b; break; // Without this statement, execution // continues on to case '/' case '/': cout << a / b; break; } return 0; }

Example 2

One excellent use of the switch statement is offering a choice to the user which formula it is desired to evaluate. Consider the well known formula for the area of a right triangle, where the area is one-half the product of the height and base of the triangle. Using algebra one could solve for any of the variables and provide a general purpose triangle program wherein the user could pick which quantity should be evaluated. One possibility is written below.

// Program to calculate needed information about a right triangle 
// The variables are: area, height, and base. 

#include <iostream.h>

int main()  { 
   char choice; 
   float area, height, base; 

   cout << "This program will evaluate right triangle formulas for:" << endl 
        << "    A    Area, given the height and base" << endl 
        << "    h    height, given the Area and base" << endl 
        << "    b    base, given the Area and height" << endl 
        << endl << endl; 
   cout << "What is your choice -- A, h, or b:"; 
   cin >> choice; 

   switch(choice)  { 
     case 'a':

     case 'A': 
     { 
	cout << endl << endl << "The height: "; 
	cin >> height; 
	cout << endl << "The base: "; 
	cin >> base; 
	area = 0.5 * base * height; 
	cout << endl << endl << "The Area is: " << area << endl; 
	break; 
     } 

// More lines follow... 

     case 'h': 
     case 'H': 
     { 
	cout << endl << endl << "The Area: "; 
	cin >> area; 
	cout << endl << "The base: "; 
	cin >> base; 
	height = 2.0 * area / base; 
	cout << endl << endl << "The height is: " << height << endl; 
	break; 
     } 

     case 'b': 
     case 'B': 
     { 
	cout << endl << endl << "The Area: "; 
	cin >> area; 
	cout << endl << "The height: "; 
	cin >> height; 
	base = 2.0 * height / base; 
	cout << endl << endl << "The base is: " << base << endl; 
	break; 
     } 
   } 
   return 0; 
}

Exercises

  1. Write a program that prompts a user to enter a series of letter grades and then prints the total number of instances of each grade (i.e., A, B, C, D, or F) that the user entered.

    solution

  2. Write a program that requests and reads pass/fail values for ten students. The program prints out how many students passed and how many failed and if more than 10 students passed, the message "Raise tuition" is printed.

    solution

  3. Write a program that prompts a user to enter a number grade and then prints the equivalent letter grade (i.e., A, B, C, D, or F).

    solution