Presentation is loading. Please wait.

Presentation is loading. Please wait.

Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.

Similar presentations


Presentation on theme: "Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2."— Presentation transcript:

1 Other Conditional Methods

2 Conditional Operator

3 Conditional Operator (?:) 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 integral value

12 switch Structures switch expression evaluated – 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 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) ExpressionValueExpressionValue age > 18True!(age > 18)False weight == 150False!( weight == 150)True

24 OR || || : OR : Logical Or operator – True if either or both operands is true Examples (assume age = 24, weight = 140) A || BValueExpression AValueExpression BValue (age > 34) || (weight <= 140)Trueage > 34Falseweight <= 140True (age > 34) || (weight >= 150)Falseage > 34Falseweight >= 150False

25 AND && && : AND : Logical And operator – True if both operands are true Examples (assume age = 24, weight = 140) A && BValueExpression AValueExpression BValue (age > 18) && (weight <= 140)Trueage > 18Trueweight <= 140True (age > 18) && (weight > 140)Falseage > 18Trueweight >= 150False

26 When to use: Yuck: Same code Expressed Twice!!!

27 When to use: Better: Conditional expresses logic for leap year in one expression

28 When to use: Yuck: Same information Expressed Twice!!!

29 When to use: Better: Each boundary only defined once

30 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))

31 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 )

32 This does not work: 0 <= num <= 10 num = 20 Compound Warning

33 Each half of compound conditional MUST stand alone: NO!!! if(60 <= grade < 90) YES if(grade = 60)

34 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

35 Short Circuit Short circuit used to "guard" an operation: bool result = fileExists && fileContains("assign")… Don't read if doesn't exist

36 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

37 Logical Equivalencies Many ways to write same thing !( x > 5)x <= 5 Use easiest form – No: !( x != 5) – Yes: x == 5

38 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

39 Precedence What goes when? – Left to right in category

40 Parens Parentheses are Good TM (true || true) && falsetrue || true && false

41 Parens Parentheses are Good TM (true || true) && falsetrue || true && false (true) && falsetrue || false && before || without parens!

42 Parens Parentheses are Good TM (true || true) && falsetrue || true && false (true) && falsetrue || false false true Parens make desired ordering obvious


Download ppt "Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2."

Similar presentations


Ads by Google