CS 1440 Lab 9

## Activity 9-2:

### Activity Goals

• Review when to use the if statement and when to use the if-else statement
• Introduce the switch statement

## When to use if and when to use if..else

Suppose you sell widgets at the following rates: \$50/widget for 10 or fewer; \$49/widget for more than 10 but no more than 20; \$48/widget for more than 20. We could use the following code to assign the unit price. (Assume that x is the number of widgets.)

```if(x<=10)rate=50;
if(x>10 && x<=20)rate=49;
if(x>20)rate=48;
```

This is not a good approach for two reasons:

• It is easy to make the three conditions either overlap or fail to cover all possible values of x.
• It is inefficient.

To illustrate the first drawback, what if the code had been

```if(x<=10)rate=50;
if(x>10 && x<20)rate=49;
if(x>20)rate=48;
```

Then if the user entered 20 for x, rate would be undefined! This can lead to serious bugs. What about

```if(x<=10)rate=50;
if(x>=10 && x<=20)rate=49;
if(x>20)rate=48;
```

Now rate is assigned two values if x is 10, and the second value (49) will be the final value of rate.

The inefficiency is due to repetition. For example, suppose x is 5. Then since 5 <= 10, the first statement assigns the proper value of rate. However, the next two statements are also executed. Although these statements don't change the value of rate, we are forced to evaluate the expressions x>10 && x<=20 and x>20 although we know in advance that we will come up with false.

When your goal is to take one of several mutually-exclusive courses of action, a better approach is to use the if..else statement (possibly nested several times). For example, here's an improved version of the previous code:

```if(x<=10)rate=50;
else if(x<=20)rate=49;
else rate=48;
```

Note that

• There is less to evaluate. If the first condition fails, then we know that x>10 and we don't have to specify this condition.
• As soon as a condition is true, rate is assigned a value and no further conditions need to be evaluted. For example, if x is 5 we can skip over the last two lines.
• No matter what x is, exactly one of the three rates will be assigned.

So when is it appropriate to use multiple if statements? If we're checking independent conditions, then this is the way to go. For example, suppose that if a person's income is greater than \$50,000, then his credit limit is raised by \$2000, and if his debt is less than \$5000 his limit is also raised by \$2000. Then we'd say

```if(income>50000)credit+=2000;
if(debt<5000)credit+=2000;
```

We don't want else here, since it is possible that the credit limit will be raised by \$4000.

When we have several mutually-exclusive conditions, we can nest if..else statements as follows:

```if(cond1)statement1;
else if(cond2)statement2;
else if(cond3)statement3;
...
else statementn;
```

## The switch Statement

In some cases, an alternative statement may be used. Suppose that we want to print "two", "four", "six", or "other" according to whether the integer x is 2, 4, 6 or none of these. In other words, we want our program's behavior to vary, depending on the value of x. We might say that the program's behavior switches, depending on the value of x. Here's the switch statement in action:
```switch(x)
{
case 2:
cout << "two";
break;
case 4:
cout << "four";
break;
case 6:
cout << "six";
break;
default:
cout << "other";
break;
}
```

The tags "case 2", ..., "default" indicate the alternatives. Following "case", you must put an integer constant; for example, you can't say

```case y>7:
```

(If you want to classify ranges of values rather than discreet values, use the if..else structure.) However, you can put an expression in the switch part; e.g.

```switch(2*x-1)
```

The default tag takes care of values of the switch not otherwise listed. You don't have to have a default part, in which case only the listed tags will be handled. break exits the switch statement. If you omit break, then the following case will be executed as well.

While you can't specify ranges in the form e.g. x >=2 && x<=4, you can list all intermediate values. For example:

```switch(x)
{
case 2:
case 3:
case 4:
cout << "[2,4]";
break;
...
}
```

### Activity Followup

Write a program named menu.C that displays a menu of choices, gets a choice from the user, and then displays which choice was made using a switch statement. Make up your own menu of choices. You get to decide whether the choice is a character or an integer.