Saturday, December 11, 2010

the switch statement

The next branching statement is called a switch statement. A switch statement is used in place of many if statements.
Let's consider the following case: Joel is writing a program that figures interest on money that is held in a bank. The amount of interest that money earns in this bank depends on which type of account the money is in. There are 6 different types of accounts and they earn interest as follows:
account typeinterest earned
personal financial2.3%
personal homeowner2.6%
personal gold2.9%
small business3.3%
big business3.5%
gold business3.8%
One way for Programmer to write this program is as follows: (assuming also that Programmer has assigned numbers to the account types starting with personal financial and ending with gold business.)
// declare a variable to keep track of the interest
float interest = 0.0;

// decide which interest rate to use.
if (account_type == 1){
  interest = 2.3;
  }
else {
     if (account_type == 2) {
       interest = 2.6;
       }
     else {
          if (account_type == 3){
            interest = 2.9;
            }
          else {
               if (account_type == 4){
                 interest = 3.3;
                 }
               else {
                    if (account_type == 5){
                      interest = 3.5;
                      }
                    else {
                         // account type must be 6
                           interest = 3.8;
                         }
                    }
               }
          }
     }
That code is hard to read and hard to understand. There is an easier way to write this, using the switch statement. The preceding chunk of code could be written as follows:
switch (account_value){
  case 1:
    interest = 2.3;
    break;
  case 2:
    interest = 2.6;
    break;
  case 3:
    interest = 2.9;
    break;
  case 4:
    interest = 3.3;
    break;
  case 5:
    interest = 3.5;
    break;
  case 6:
    interest = 3.8;
    break;
  default:
    interest = 0.0;
}
The switch statement allows a programmer to compound a group of if statements, provided that the condition being tested is an integer. The switch statement has the form:
switch(integer_val){
   case val_1:
      // code to execute if integer_val is val_1
      break;
    ...
   case val_n:
      // code to execute if integer_val is val_n
      break;
   default:
      // code to execute if integer_val is none of the above
}
The default clause is optional, but it is good programming practice to use it. The default clause is executed if none of the other clauses have been executed. For example, if my code looked like:
switch (place) {
   case 1:
      cout << "we're first" << endl;
 break;
   case 2:
      cout << "we're second" << endl;
 break;
   default:
 cout << "we're not first or second" << endl;
}
This switch statement will write "we're first" if the variable place is equal to 1, it will write "we're second" if place is equal to 2, and will write "we're not first or second" if place is any other value.
The break keyword means "jump out of the switch statement, and do not execute any more code." To show how this works, examine the following piece of code:
int value = 0;
switch(input){
  case 1:
    value+=4;
  case 2:
    value+=3;
  case 3: 
    value+=2;
  default:
    value++;
}
If input is 1 then 4 will be added to value. Since there is no break statement, the program will go on to the next line of code which adds 3, then the line of code that adds 2, and then the line of code that adds 1. So valuewill be set to 10! The code that was intended was probably:
int value = 0;
switch(input){
  case 1:
    value+=4;
    break;
  case 2:
    value+=3;
    break;
  case 3: 
    value+=2;
    break;
  default:
    value++;
}
This feature of switch statements can sometimes be used to a programmers' advantage. In the example with the different types of bank accounts, say that the interest earned was a follows:
account typeinterest earned
personal financial2.3%
personal homeowner2.6%
personal gold2.9%
small business2.6%
big business2.9%
gold business3.0%
Now, the code for this could be written as:
switch (account_value){
  case 1:
    interest = 2.3;
    break;
  case 2:
  case 4:
    interest = 2.6;
    break;
  case 3:
  case 5:
    interest = 2.9;
    break;
  case 6:
    interest = 3.8;
    break;
  default:
    interest = 0.0;

No comments:

Post a Comment