Download presentation
Presentation is loading. Please wait.
1
Other Conditional Methods
2
Conditional Operator
3
Conditional Operator (?:)
Ternary operator: 3 arguments expression1 ? expression2 : expression3 If expression1 is true, result is expression2 false, result is expression3
4
Conditional Operator (?:)
Example: int max = (a >= b) ? a : b; Short for: int max; if (a >= b) max = a; else max = b;
5
Common Use Pick one of two values inside larger expression:
6
Switch Statement
7
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"; …
8
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"; …
9
Comparing if…else with ifs
Method 2 is more efficient… but messy
10
Switch Statement switch choses from list of integer options
If 1 do this If 2 do this If 3 do this …
11
switch Structures switch expression is evaluated first
Must be integer value
12
switch Structures switch expression evaluated Jump to matching value
Must be integral value Jump to matching value
13
switch Structures switch expression is evaluated first
Must be integral value Jump to matching value Execute any instructions in that case
14
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 }
15
switch version Switch month select:
int month = magic value switch(month) { case 1: cout << "Jan"; break; case 2: cout << "Feb"; break; … }
16
switch version int month = magic value switch(month) { case 1: cout << "Jan"; cout << "1st month"; break; case 2: … }
17
switch version Default is the "else" switch(month) { … case 11: cout << "Nov"; break; case 12: cout << "Dec"; break; default: cout << "Unknown month"; }
18
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; … }
19
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"; }
20
Switch Gotchas No way to represent ranges: switch (day) {
case <= 5: cout << "Weekday"; //NO Easy to mess up forget break statement
21
Logical Operators
22
If you make over $35000 and are married
Boolean Operators Compound condition: If you make over $35000 and are married Boolean/Logic Operators combine/manipulate Boolean values
23
Not ! ! : Not : Logical unary negation operator
Evaluates to opposite of input False True and True False Examples (assume age = 24, weight = 140) Expression Value age > 18 True !(age > 18) False weight == 150 !( weight == 150)
24
OR || || : OR : Logical Or operator
True if either or both operands is true Examples (assume age = 24, weight = 140) A || B Value Expression A Expression B (age > 34) || (weight <= 140) True age > 34 False weight <= 140 (age > 34) || (weight >= 150) weight >= 150
25
AND && && : AND : Logical And operator True if both operands are true
Examples (assume age = 24, weight = 140) A && B Value Expression A Expression B (age > 18) && (weight <= 140) True age > 18 weight <= 140 (age > 18) && (weight >= 150) False weight >= 150
26
When to use: Same code Expressed Twice!!! Yuck:
27
When to use: Better: Conditional expresses logic for leap year in one expression
28
When NOT to use: Yuck: Same information Expressed Twice!!!
double score = 75; if (score >= 90.0) cout << "Grade is A"; if (score < 90.0 || score >= 80.0) cout << "Grade is B"; if (score < 80.0 || score >= 70.0) cout << "Grade is C"; if (score < 70.0 || score >= 60.0) double score = 75; if (score < 90.0 && score >= 80.0) if (score < 80.0 && score >= 70.0) if (score < 70.0 && score >= 60.0) cout << "Grade is D"; if (score < 60.0) cout << "Grade is F";
29
When NOT to use: Better: Each boundary only defined once
double score = 75; if (score >= 90.0) cout << "Grade is A"; else if (score >= 80.0) cout << "Grade is B"; else if (score >= 70.0) cout << "Grade is C"; else if (score >= 60.0) cout << "Grade is D"; else cout << "Grade is F";
30
Booleans Nasty Details
31
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
32
Short Circuit Short circuit used to "guard" an operation:
bool result = fileExists && fileContains("assign")… Don't read if doesn't exist
33
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
34
Precedence What goes when? Left to right in category
35
Parens Parentheses are GoodTM (true || true) && false
36
Parens Parentheses are GoodTM && before || without parens!
(true || true) && false true || true && false (true) && false true || false && before || without parens!
37
Parens Parentheses are GoodTM Parens make desired ordering obvious
(true || true) && false true || true && false (true) && false true || false false true Parens make desired ordering obvious
38
Order of ops important! ( ) !
relational operators ==, <, >, etc… && 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 )
39
Compound Warning This does not work: 0 <= num <= 10 num = 20
40
Compound Warning Each half of compound conditional MUST stand alone:
NO!!! if(60 <= grade < 90) YES if(grade <= 60 && grade < 60)
41
Logical Equivalencies
Many ways to write same thing !( x > 5) x <= 5 Use easiest form No: !( x != 5) Yes: x == 5
42
DeMorgan's Can convert AND OR Use to simplify logic:
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.