# Simple Selections

Tom Kelliher, CS17

Mar. 11, 1996

We now begin to add decision making capabilities to our programs.

Consider the tax computation program:

• Assume that incomes are between \$50,000 and \$80,000.
• Federal tax is \$5,100 plus 28% of gross income over \$34,000.
Generalize for any income.

```federalTax = FEDERAL_BASE
+ (grossIncome - FEDERAL_EXEMPTION) * FEDERAL_RATE;
```

Or:

```if (grossIncome <= FEDERAL_EXEMPTION)
federalTax = FEDERAL_BASE;
else
federalTax = FEDERAL_BASE
+ (grossIncome - FEDERAL_EXEMPTION) * FEDERAL_RATE;
```
What was the root of the problem with the first fragment?

C++'s selection statements:

• if --- two-way, one-way, multi-way
• switch --- multi-way

# Two-Way Selection

Form:

```if (<Expression>)
<ExpressionTrueStatement>;
else
<ExpressionFalseStatement>;
```
1. <Expression>:
1. Named: condition, predicate.
2. Any expression with a numerical value.
3. Non-zero value interpreted as ``true.''
4. Zero value interpreted as ``false.''
5. First thing evaluated in performing if statement.
2. <ExpressionTrueStatement>
1. Evaluated if <Expression> interpreted as true.
2. Single statement or a compound block `{ ... }`.
3. <ExpressionFalseStatement>
1. Evaluated if <Expression> interpreted as false.
2. Single statement or a compound block `{ ... }`.
4. <Expression> always evaluated, exactly one of the other two is evaluated.

## Equality Operators

• == --- has value 1 if operands are exactly equal, otherwise has value 0.

Easily confused with =:

```int i = 3;

if (i == 3)
cout << "i is 3.\n";
else
cout << "i isn't 3.\n";

i = 6;

if (i = 3)
cout << "i is 3.\n";
else
cout << "i isn't 3.\n";
```
• != --- has value 1 if operands are not equal, otherwise has value 0.
```int i = 3;

if (i != 3)
cout << "i isn't 3.\n";
else
cout << "i is 3.\n";
```
Same effect as == if?

## Relational Operators

1. <
2. <=
3. >
4. >=
Each binary operator evaluates to 0 or 1.

Examples:

```int i = 4, j = 4, k = 5, l;

l = i == j;
l = i > k;
cout << (l < j) << endl;   // parentheses required --- see next section
i = 4;
j = 10;
k = 6;
l = i < j < k;   // left associative
```

## Simplifying the Predicate

Consider deciding if a number is divisible by 10:

```if (n % 10 == 0)
cout << "Divisible by 10.\n";
else
cout << "Not divisible by 10.\n";
```
Simplified:
```if (n % 10)
cout << "Not divisible by 10.\n";
else
cout << "Divisible by 10.\n";
```
Same effect.

Simpler?

# New Precedence Table

1. () (left)
2. Unary +, -, ++, -- (right)
3. *, /, % (left)
4. Binary +, - (left)
5. `<<`, `>>` (left)
6. <, <=, >, >= (left)
7. ==, != (left)
8. Assignment =, +=, -=, *=, /=, %= (right)

# Multi-Way Selection

## Filing Status for a Tax Program

```if (filingStatus == 1)
cout << "Single";
else
if (filingStatus == 2)
cout << "Married filing jointly";
else
if (filingStatus == 3)
cout << "Married filing separately";
else
if (filingStatus == 4)
else
cout << "Error in filing status;";
```
Exactly one cout taken.

```if (filingStatus == 1)
cout << "Single";
else if (filingStatus == 2)
cout << "Married filing jointly";
else if (filingStatus == 3)
cout << "Married filing separately";
else if (filingStatus == 4)
else
cout << "Error in filing status;";
```

## Selection based on two variables

• Customer has good credit standing (value above 6):
• payment is balance if balance under \$20.01.
• payment if \$10 if balance between \$20.01 and \$100.00
• else payment is 10% of balance.
• Customer has poor credit standing:
• payment is balance.

```if (creditStanding > 6)
{
if (balance <= 20.0)
minPayment = balance;
else if (balance <= 100)
minPayment = 10.0
else
minPayment = 0.1 * balance;
}
else
minPayment = balance;
```
Compound block necessary due to structure of problem.

# Exercises

Chapter 5 Study Guide Programming Exercises 1, 2, 3, 5, 9.

Thomas P. Kelliher
Sun Mar 10 10:43:01 EST 1996
Tom Kelliher