Simple Selections

Tom Kelliher, CS17

Mar. 11, 1996

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

Consider the tax computation program:

Generalize for any income.

How about:

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:

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

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?

Easier to read?

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)
            cout << "Head of household";
         else
            cout << "Error in filing status;";
Exactly one cout taken.

Easier to read:

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)
   cout << "Head of household";
else
   cout << "Error in filing status;";

Selection based on two variables

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