Decision Statements (Switch Case)


There are many instances wherein you may want to test for a series of conditions one after the other. For example: Suppose you obtain the input of month from the user in as a number and you want to display the corresponding name of the month; what would you do? You could write a series of twelve ‘if….else if….else if…else if…’ statements.

The ‘switch…case’ format provides a convenient alternative to using multiple ‘if-else’ statements when you are testing for a single variable alone. The switch statement successively tests the value of a variable against a list of integer or character constants. If a match is found then the statement associated to that particular case will be executed.

First of all, before entering the switch case statement, we have to obtain the value of the switch variable from the user. The switch variable refers to the variable whose value you want to check. In the case of converting numbers into corresponding months, the switch variable will be the month number.

Syntax:

For example:

      # include <iostream.h>
      int main ( )
      {
          int month;
          cout<< "Enter the month of the year: ";
          cin>>month;
          switch (month) // month is the switch variable
         
      {
              case 1 :                 // if month is 1 then the statements are executed
            
      {
                  cout<<"The month is January";
                  break;
              }
             case 2 :
            {
                   cout<<"The month is February";
                   break;
            }
      //write case statements for 3 to 10 just as shown above
           
      case 11 :
            {
                   cout<<"The month is November";
                   break;
             }
             case 12 :
            {
                   cout<<"The month is December";
                   break;
             }
            default :                 // If value of day is something other than 1 to 7
             
      {
                    cout<<"You entered an invalid number";
                    break;
              }
         }
         return 0;
      }

As you can see, the compiler gets the value of the variable ‘month’ from the user. This is called the ‘switch variable.’ If the value of ‘month’ is 1, then the compiler performs what is specified under case 1. If the user enters 2, then the output will be February and so on. Since a year has only 12 months, if the user types 0 or 14 then the program should display that the user has typed the wrong number. The ‘default’ statement is used for this purpose. The ‘default’ statement provides the program with an option to do something in case the switch variable does not match any of the case constants.

In this program, ‘month’ is called the switch variable and the integer constants from 1 to 12 are called case constants (i.e. they are the values for which the switch variable is tested).


Break: It causes an exit from the switch construct (body). For instance, after printing that "The month is January" you don’t want the compiler to go into the other cases. Hence you ask it to break out of the ‘switch…case’ body. If there is no break for each case then the program will perform all the remaining cases as well.

Try it: Remove all the break statements from the above program and execute your program. If you now type a value of 1, the program will print:

Beware: A mistake that beginners commit is that they tend to forget the ‘break’ statement. Always use ‘break’ after each case and also make sure that you have a ‘default’ option in your ‘switch…case’ body.

The ‘default’ case is executed only if the user enters a value other than the case constants. It does not matter whether you place the default case at the starting of the switch-case body or at the end (but usually programmers prefer to place the default case at the end of the switch-case construct).

Suppose you want to test the switch variable against a set of character constants then ensure that you enclose your case constants within single quotes.

Suppose the switch variable ‘month’ is a character then the program would be:

Beware: There can be an expression in the switch part but it should evaluate to an integer. The switch variable and the case constants should be integers (or characters). You should not use other data types.


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.