Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.

Similar presentations


Presentation on theme: "Selection in C++ If statements. Control Structures Sequence Selection Repetition Module."— Presentation transcript:

1 Selection in C++ If statements

2 Control Structures Sequence Selection Repetition Module

3 Selection If or if / else statement choosing between mutually exclusive possibilities Two forms  if (logical expression) statement; else statement;

4 Logical Expressions Relational Operators ==, !=, =, >, < They are binary operators – have two operands work on any type of data, be careful about matching types watch out for = and == produce a bool result

5 Syntax of if statement if (logical expression) statement; Don't put semicolon after condition Indentation helps humans, doesn't matter to compiler

6 Syntax of if statement if (x > 0) y = sqrt(x); if (x > y) t = x; x = y; y = t; // what goes with the if?

7 If-Then Statement Determine whether or not to execute a statement (which can be a single statement or an entire block) TRUE FALSE statement expression

8 Examples output larger of two numbers don't allow sqrt of negative number don't allow overflow determining even or odd give student another chance at a question

9 Visual Aid Decision tree  “if person is 18 or over and state is KY, they can have regular license”  “if person is 16 or over and under 18 and state is TN or VA, they can have learner’s permit”  “if person is under 16, in any state, they can’t have license”  “if person is over 65 in VA, they have modified license”

10 Example What is output? Why? int age; age = 20; if (age = 16) { cout << “Did you get driver’s license?”; }

11 Example What is output? Why? int age; age = 30; if (age < 18) cout << “Do you drive?”; cout << “Too young to vote”;

12 Example What is output? Why? int number; number = 0; if (number = 0) cout << “Zero value”; else cout << “Non-zero value”;

13 Logical Operators And (&&) Or (||) Not (!) && and || are binary operators, ! is unary used to combine bool values produce a bool result truth tables operator precedence  ! then && then ||  ! is very high, above most operators  && and || are very low, below other operators

14 OperatorMeaning Associativity ! NOTRight *, /, % Multiplication, Division, Modulus Left +, - Addition, SubtractionLeft < Less thanLeft <= Less than or equal toLeft > Greater thanLeft >= Greater than or equal toLeft == Is equal toLeft != Is not equal to Left && ANDLeft || OR Left = AssignmentRight 14

15 Logical Expression MeaningDescription ! p NOT p ! p is false if p is true ! p is true if p is false p && q p AND q p && q is true if both p and q are true. It is false otherwise. p || q p OR qp || q is true if either p or q or both are true. It is false otherwise.

16 Converting English to logic "0 is less than x is less than 5" "x is 5 or 6" "x is bigger than 5 and less than 10" impossible situations "dead code"  always true – “x 5 || x < 8”  always false – “x 10”

17 Write an expression for each taxRate is over 25% and income is less than $20000 temperature is less than or equal to 75 or humidity is less than 70% age is over 21 and age is less than 60 age is 21 or 22

18 Some Answers (taxRate >.25) && (income < 20000) (temperature <= 75) || (humidity <.70) (age > 21) && (age < 60) (age == 21) || (age == 22)

19 What went wrong? This is only supposed to display “HEALTHY AIR” if the air quality index is between 50 and 80. But when you tested it, it displayed “HEALTHY AIR” when the index was 35. int AQIndex; AQIndex = 35; if (50 < AQIndex < 80) cout << “HEALTHY AIR“;

20 Analysis of Situation AQIndex = 35; According to the precedence chart, the expression (50 < AQIndex < 80) means (50 < AQIndex) < 80 because < is Left Associative (50 < AQIndex) is false (has value 0) (0 < 80) is true.

21 Corrected Version int AQIndex; AQIndex = 35; if ((50 < AQIndex) && (AQIndex < 80)) cout << “HEALTHY AIR“;

22 Nested if's the statement to be executed in an if statement can be another if an else branch goes with the nearest if "dangling else"

23 Example float average; average = 100.0; if (average >= 60.0) if (average < 70.0) cout << “Marginal PASS”; else cout << “FAIL”; FAIL is printed; WHY? The compiler ignores indentation and pairs the else with the second if average 100.0

24 if.. else provides two-way selection between executing one of 2 clauses (the if clause or the else clause) TRUE FALSE if clauseelse clause expression

25 What happens if you omit braces? if ((carDoors == 4) && (driverAge > 24)) premium = 650.00; cout << “ LOW RISK “; else premium = 1200.00; cout << “ HIGH RISK ”; monthlyPayment = premium / 12.0 + 5.00; Compile error occurs: The “if clause” is the single statement following the if

26 Example  Given x, y on Cartesian plane, which quadrant is it in? (I, II, III, IV) if (x > 0) if (y > 0) quadrant = 1; else quadrant = 4; else if (y > 0) // NOT a redundant test! quadrant = 2; else quadrant = 3;

27 Another form of nested if's if (condition) statement; else if (condition) statement; else if (condition) statement; else statement; // good for mutually exclusive and exhaustive conditions

28 Comparing Strings Two objects of type string (or a string object and a C string) can be compared using the relational operators A character-by-character comparison is made using the ASCII character set values If all the characters are equal, then the 2 strings are equal. Otherwise, the string with the character with smaller ASCII value is the “lesser” string

29 string myState; string yourState; myState = “Texas”; yourState = “Maryland”; ExpressionValue myState == yourState false myState > yourState true myState == “Texas”true myState < “texas”true

30 Testing code needs to be tested in every possible branch takes multiple runs test plans CaseInputOutput (expected) Output (actual)

31 Test Plan if (x + y > 0) cout << "safe"; else if (x > 3) cout << "high"; else cout << "low";

32 Test Plan CaseInputOutput (expected) Output (actual) x + y > 0x = 5, y = 7"safe" x + y 3 x = 5, y=-7"high" x+y <=0, x <= 3 x = 2, y=-3"low"

33 Comparing Real Values Do not compare floating point values for equality, compare them for near-equality. float myNumber; float yourNumber; cin >> myNumber; cin >> yourNumber; if (fabs (myNumber - yourNumber) < 0.00001) cout << “They are close enough!” << endl;


Download ppt "Selection in C++ If statements. Control Structures Sequence Selection Repetition Module."

Similar presentations


Ads by Google