Notes
Outline
COM1100
Fundamentals of Computer Science –Winter 2000
Lecture 12
02/07/00
Lecture today
Multiple selection
if-else chain
switch statement
Fancy compound operators
y += 5;
Equals to:  y = y + 5;
joe -= 6;
Equals to: joe = joe –6;
x *= y;
Equals to: x = x * y;
w /= 15;
Equals to: w = w /15;
r %=3;
Equals to: r = r % 3;
Multiple Selection – if-else chain
if ( condition_1 )
statement1;
else if ( condition _2  )
statement2;
else if ( condition _3 )
statement3;
  …
else if (condition _n )
statementn;
else
last statement;
How it works?
Each condition is evaluated in the order in which it appears in the statement
If the first condition is true, the corresponding statement is executed and the remainders of the statement in the chain are not executed.
The final else is optional.
The last statement is only executed if none of the previous condition were true.
Example
Write a program that will assign a grade for the exam
grade < 60 is F
60 <= grade < 80 is C
80 <= grade < 90 is B
90 <= grade is A
Another example
char marcode;
cout << "Please enter a marcode (M,S,D):";
cin >> marcode;
if(marcode == 'M')
cout << "Married" << endl;
else if (marcode == 'S')
cout << "Single" << endl;
else if (marcode == 'D')
cout << "Divorced" << endl;
else
cout << "Invalid code" << endl;
switch statement
 switch ( expression )
{ // start of the compound statement
case value_1 :
statement 1;
break;
case value_2 :
statement 1;
break;
…
case value_n :
statement n;
break;
default :
statement;
} // end of  the compound statement
What each of these words does?
switch
identifies the start of the switch statement.
The expression in parentheses following this word is evaluated and the result of the expression is compared to various alternative values contained with the compound statement.
The expression in the switch statement must be evaluated to an integer.
Otherwise, you may get a compiling error message.
What each of these words does?
case
identifies or labels individual values that are compared to the value of the switch expression.
switch expression’s value is compared to each of these case values in the order in which these values are listed until a match is found.
What each of these words does?
default
Any number of case labels maybe contained within a switch statement in any order.
If the value of the expression doesn’t match any of the case value, it will look for default and program execution begins with the statement following the word default.
What each of these words does?
break
Once an entry has been located by the switch statement, all further case evaluations are ignored and execution continues until a break statement is encountered.
So break identifies the end of a particular case and causes an immediate exit from the switch statement.
Example
char marcode;
cout << "Please enter a marcode (M,S,D):";
cin >> marcode;
switch (marcode)
{
case 'M':
cout << "Married" << endl;
break;
case 'S':
cout << "Single" << endl;
break;
case 'D':
cout << "Divorced" << endl;
break;
default:
cout << "Invalid code" << endl;
}