SELECTION STATEMENTS

RELATIONAL OPERATORS IN C++:

<

>

<=

>=

==

!=

-Important to note that an expression that evaluates to TRUE produces the value 1 and a FALSE expression produces the value of 0. (In fact, any non-zero value is considered true -- watch out!)

BOOLEAN OPERATORS IN C++:

&& - AND

|| - OR

! - NOT

OPERATOR PRECEDENCE IN C++:

(see pg. 198)

!

/,*,%

+, -

<, >, <=, >=

==, !=

&&

||

=, +=, *=, -=, /=, %=

**REMEMBER SHORT-CIRCUIT EVALUATION!

IF STATEMENTS IN C++:

-SIMPLE:

```if (condition)
statement;
```

-note ( )'s surrounding condition - required!

-Keyword "if" is lowercase

-statement could be a COMPOUND STATEMENT if more than one statement is needed

-GENERAL:

```if (conidition)
statement;
else
statement;
```

-again, statement could be compound

**Watch indentation problems! { }'s determine begin/end of if/else clauses in case of compound statements!

EXAMPLES:

```x = 3;
if (x > 10)
x++;
cout << x; <====This is NOT part of if clause!
cout << "done!";

```

-if you want the output to be part of the if clause:

```x = 3;
if (x > 10)
{
x++;
cout << x;
}
cout << "done";

```

-NESTING IF'S:

```if (x > 0)
cout << "x is positive";
else
if (x < 0)
cout << "x is negative";
else
cout << "x is zero";

```

-watch for DANGLING ELSE problem:

```if (x > 0)
if (y < 0)
cout << "y is negative";
else
cout << "x is positive";

```

-the else, above, is really part of second if statement:

```if (x > 0)
if (y < 0)
cout << "y is negative";
else
cout << "x is positive";

```

-if you want it to be part of the first statement:

```if (x > 0)
{ //make a compound statement
if (y < 0)
cout << "y is negative";
}
else
cout << "x is positive";

```

-MULTIPLE NESTED IF-ELSE:

```cin >> grade;
letter = 'A';
else
letter = 'B';
else
if (grade >= 70)
letter = 'C';
else
if (grade >= 60)
letter = 'D';
else
letter = 'F';

```

-OR CAN "COMBINE" IF CLAUSES AS PART OF NEXT ELSE:

```cin >> grade;
letter = 'A';
else   if (grade >=80)
letter = 'B';
else if (grade >= 70)
letter = 'C';
else if (grade >= 60)
letter = 'D';
else
letter = 'F';

```

-EXECUTION OF BOTH NESTED STATEMENTS IS SAME - ONLY STYLE IS DIFFERENT

-advantage of 1st style is can clearly see separate else clauses and their associated values

-advantage of 2nd style is for deeply nested statements, the indentation doesn't get out of hand and can clearly see separate cases

EXERCISES:

-if the variable TaxCode is 'T', increase the variable Price by adding TaxRate times Price to it

-If the variable Code is 1, read values for variables X and Y and print their sum

-If the variable A is strictly between 0 and 5, set the variable B to 1/A2, otherwise set B to A2

-write a function which returns the greater of two integer values passed to it

-write a function which prints the day of the week given an integer parameter Day (1 - 7)

-write a function which returns the value 'Cost', given an integer parameter Distance, and this table:

distance cost
 ```0 thru 100 ``` `5.00` ```more than 100 but not more than 500 ``` `8.00` ```more than 500 but less than 1000 ``` `10.00` ```1000 or more ``` `12.00`

```
```

(watch conditions carefully!!)

AN ALTERNATIVE SELECTION STATEMENT IN C++: THE SWITCH STATEMENT

-an alternative multiple selection statement

-not general purpose

-used when decision is being based on a single value (which must be integer/int compatible - no real numbers or strings) which has bounded ranges (not simply >, < some value)

```switch (expression)
{
caselist1 : statementlist1
caselist2 : statementlist2
...
default : statementlistN
}

```

-switch is a C++ keyword

-expression is some integer type or char, must be in ( )'s

-body of statement is enclosed in { }'s

-caselist is a sequence of cases with this form: case constantvalue :

where case is a keyword and the constant value is of same/compatible type as expression

-statementlist is a sequence of C++ statements

-default is a C++ keyword, and is optional, usually used for error reporting

How It Works:

-The value of expression is evaluated

-if its value is in one of the caselists, the corresponding statementlist is executed

-subsequent statementlists will also be executed, unless a return or break statement is encountered

-if the value of expression is not in one of the caselists, the statementlist corresponding to the default case is executed. If there is no default case, execution "falls through" the switch statement, with no statementlists being executed, and program continues with next statement after the switch

The break statement:

-used to "interrupt" current program construct, and continue program execution at the next construct following

-used almost exclusively in switch statements

-don't need in last case (default or otherwise)

-don't need if statementlist concludes with a return statement, since that "sends" program back to calling function, anyway

EXAMPLES:

switch(DayNum)

{

case 1 : cout << "Sunday";

break;

case 2 : cout << "Monday";

break;

case 3 : cout << "Tuesday";

break;

case 4 : cout << "Wednesday";

break;

case 5 : cout << "Thursday";

break;

case 6 : cout << "Friday";

break

case 7 : cout << "Saturday";

break;

default : cout << "Day number out of range";

}

(show output w/o breaks!)

---------------------------------------------------

switch (Channel)

{

case 2 : cout << "KREM"; break;

case 3 : cout << "KAYU"; break;

case 4 :

case 14: cout << "KXLY"; break; //both 4 and 14 will print KXLY

case 6 : cout << "KHQ"; break;

case 7 : cout << "KSPS";

}

----------------------------------------------------

Problem: suppose we have to print the discount for wholesale customers for Widgets R Us, based on this table:

number of orders discount customer category

1 - 9 0% preferred customer

10-19 3% outlet

20-49 5% local distributor

50-99 7% regional distributor

100-499 10% international distributor

-This would make for some LONG case lists:

case 1: case 2: case 3:.......case 9 :

(and what about that last case? ICK!)

-so we'll outsmart the syntax:

```switch (order / 100)
{
case 0 : switch (order / 10)
{
case 0 : Discount = 0.0; break;
case 1 : Discount = 0.03; break;
case 2: case 3: case 4: Discount = 0.05;break;
case 5: case 6: case 7: case 8: case 9: Discount = 0.07;
}
case 1: case 2: case 3: case 4: Discount = 0.1;
}
```

-----------------------------------------------------------------------------------------------------------------

-What about if we want to use a real value expression? For example, printing the letter grade,

based on numeric average? Once again, we'll outsmart the syntax:

switch (int(Average) / 10)

{

case 9:

case 10: cout << 'A'; break;

case 8 : cout << 'B'; break;

case 7 : cout << 'C'; break;

case 6 : cout << 'D'; break;

default : cout << 'F';

}

-------------------------------------------------------------------

-Convert the above to a function (showing return eliminates break):

{

switch (int(Average) / 10)

{

case 9:

case 10: return 'A';

case 8 : return 'B';

case 7 : return 'C';

case 6 : return 'D';

default : return 'F';

}

}//end function

EXERCISES:

- print the letters which appear on phone pad

0 - operator

1 - nothing

2 - ABC

3 - DEF

4 GHI

5 JKL

6 MNO

7 PRS

8 TUV

9 WXY

-write a function, with a char parameter, that returns the integer of the phone key, corresponding to the letter given ("inverse" of above example). Note that 'Q' and 'Z' are not present on phone pad. If the parameter is either of these value, return -1

-write a function, with a real parameter, 0.0 to 4.0, which returns a letter grade based on this table:

0.0 -0.9 = F

1.0 -1.9 = D

2.0- 2.9 = C

3.0 - 3.9 = B

4.0 = A

-write the "inverse" of the above function: take in a letter grade (upper or lower case!) and return a real value - strictly 0.0, 1.0, 2.0, 3.0, 4.0