Friday, July 22, 2011

Branching Statements (if, else, switch)


the if statement

The first type of branching statement we will look at is the if statement. An if statement has the form:

if (condition)
{
   // code to execute if condition is true
}
else
{
   // code to execute if condition is false
}

In an if statement, condition is a value or an expression that is used to determine which code block is executed, and the curly braces act as "begin" and "end" markers.
Here is a full C++ program as an example:

//include this file for cout
#include <iostream.h>

int main() {
  // define two integers
  int x = 3;
  int y = 4;

  //print out a message telling which is bigger
  if (x > y) {
    cout << "x is bigger than y" << endl;
  }
  else {
    cout << "x is smaller than y" << endl;
  }
  return 0;
}

In this case condition is equal to "(x > y)" which is equal to "(3 > 4)" which is a false statement. So the code within the else clause will be executed. The output of this program will be:

x is smaller than y

If instead the value for x was 6 and the value for y was 2, then condition would be "(6 > 2)" which is a true statement and the output of the program would be:

x is bigger than y

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 type
interest earned
personal financial
2.3%
personal homeowner
2.6%
personal gold
2.9%
small business
3.3%
big business
3.5%
gold business
3.8%

One way for Joel to write this program is as follows: (assuming also that Joel 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 value will 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 type
interest earned
personal financial
2.3%
personal homeowner
2.6%
personal gold
2.9%
small business
2.6%
big business
2.9%
gold business
3.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