CS 107 - Lab 4

Switch Statement

The switch statement is another way to select a section of code for execution instead of using if statements. The syntax of the switch statement is:
     switch ( condition )
        {
         case constant1:
             statement(s);
             break;

         case constant2:
             statement(s);
             break;
           .
           .
           .

         case constantN:
             statement(s);
             break;

         default :
             statement(s);
             break;


        }
The switch statement (sometimes called the case or switch-case statement) is an easy way to determine which section of code should be executed based on the result of an expression.

The default: cause is optional, but is useful to specify a section of code to be executed if none of the case values match the result of the expression.

If you want to have one section of code executed for multiple results of the expression, you can put multiple case values together as shown below. Assume total is an integer variable.

     switch ( total )
        {
         case 7:
         case 11:
             cout << "You win!" << endl;
             break;

         case 2:
         case 3:
         case 12:
             cout << "You lose!" << endl;
             break;
           .
           .
           .
        }

Lab Assignment

Due: By your Lab Time during the fifth week of the semester (the week of 2/10/2003).

You may turn in the assignment to your TA during lab or place it in his mailbox in 905 SEO. It is suggested that you place it in his mailbox just in case you are unable to attend lab. You are to hand in a print out of your program to do the following to your TA.

Create a c++ program on your UIC icarus account that will perform either addition, subtraction, multiplication, division or exponentiation on two numbers based on the result of a prompt.

Hand in a print out of your program to your TA.