other conditional methods. conditional operator conditional operator (?:) conditional operator ( ?:...
TRANSCRIPT
Conditional Operator (?:)
• Conditional operator (?:) – Ternary operator: 3 argumentsexpression1 ? expression2 : expression3
• If expression1 is – true, result is expression2– false, result is expression3
Conditional Operator (?:)
• Example: int max = (a >= b) ? a : b;
• Short for: int max;if (a >= b) max = a;else max = b;
Comparing if…else with ifs
• Method 1:if( month == 1)
cout << "Jan";if( month == 2)
cout << "Feb";if( month == 3)
cout << "Mar";if( month == 4)
cout << "Apr";…
Comparing if…else with ifs
• Method 2:if( month == 1)
cout << "Jan";else if( month == 2)
cout << "Feb";else if( month == 3)
cout << "Mar";else if( month == 4)
cout << "Apr";…
Switch Statement
• switch choses fromlist of integer options– If 1 do this– If 2 do this– If 3 do this
…
switch Structures
• switch expression is evaluated first– Must be integral value
• Jump to matching value• Execute any instructions
in that case
switch Structures
• switch expression is evaluated first– Must be integral value
• Jump to matching value• Execute any instructions
in that case• Continue until break– break jumps to }
switch version
• Switch month select:int month = magic value switch(month) {
case 1:cout << "Jan";break;
case 2:cout << "Feb";break;
…}
switch version
int month = magic value switch(month) {
case 1:cout << "Jan";cout << "1st month";break;
case 2:…
}
switch version
• Default is the "else"switch(month) {
…case 11:
cout << "Nov";break;
case 12:cout << "Dec";break;
default:cout << "Unknown month";
}
switch version
• No break – continues executing statements!– Jan and Feb!!!
int month = magic value switch(month) {
case 1:cout << "Jan";
case 2:cout << "Feb";break;
…}
switch version
• Fall through used well:
switch (day) { case 1: // Fall to through to the next case case 2: // Fall to through to the next case case 3: // Fall to through to the next case case 4: // Fall to through to the next case case 5: cout << "Weekday"; break; case 0: // Fall to through to the next case case 6: cout << "Weekend"; }
Switch Gotchas
• No way to represent ranges:switch (day) {
case <= 5: cout << "Weekday";//NO
• Easy to mess up– forget break statement
Boolean Operators
Compound condition:
If you make over $35000 and are married
• Boolean/Logic Operators combine/manipulate Boolean values
Not !
• ! : Not : Logical unary negation operator– Evaluates to opposite of input
False True and True False
Examples (assume age = 24, weight = 140)
Expression Value Expression Value
age > 18 True !(age > 18) False
weight == 150 False !( weight == 150) True
OR ||
• || : OR : Logical Or operator– True if either
or both operands is true
Examples (assume age = 24, weight = 140)
A || B Value Expression A Value Expression B Value
(age > 34) || (weight <= 140) True age > 34 False weight <= 140 True
(age > 34) || (weight >= 150) False age > 34 False weight >= 150 False
AND &&
• && : AND : Logical And operator– True if both
operands are true
Examples (assume age = 24, weight = 140)
A && B Value Expression A Value Expression B Value
(age > 18) && (weight <= 140) True age > 18 True weight <= 140 True
(age > 18) && (weight > 140) False age > 18 True weight >= 150 False
Order of ops important!
1. ( )
2. !
3. relational operators==, <, >, etc…
4. &&
5. II
Left to right in each category
if (!2 < 4) if (!(2 < 4))
Order of ops important!
1. ( )
2. !
3. relational operators==, <, >, etc…
4. &&
5. II
Left to right in each category
if (!2 < 4) if (!(2 < 4))if ( false < 4 ) if ( !(true) )
if ( 0 < 4 ) if ( false )if ( true )
Compound Warning
• Each half of compound conditional MUST stand alone:
• NO!!!
if(60 <= grade < 90)
• YES
if(grade < 90 && grade >= 60)
Short Circuit
• C++ only evaluates right half of logical statement if needed– Short Circuit evaluation
bool result = x > 5 && y == 2;
Not evaluated if x <= 5
Short Circuit
• Short circuit used to "guard" an operation:bool result = fileExists && fileContains("assign")…
Don't read if doesn't exist
Short Circuit
• Short circuit can cause bugs:– Avoid side effects in second statement
bool result = x > 2 && y++ > 1;
x > 2, we add one to y
x <= 2, nothing happens to y
Logical Equivalencies
• Many ways to write same thing
!( x > 5) x <= 5
• Use easiest form– No: !( x != 5)– Yes: x == 5
DeMorgan's
• Can convert AND OR– Not both means not one or not the other!(x && y) == !x || !y
– Not either means not one and not the other!(x || y) == !x && ! y
• Use to simplify logic:!(x != 5 || y > 2)!(x !=5) && !(y > 2)x == 5 && y <= 2
Parens
• Parentheses are GoodTM
(true || true) && false
true || true && false
(true) && false
true || false
&& before || without parens!