Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Logic October 6, 2017.

Similar presentations


Presentation on theme: "Conditional Logic October 6, 2017."— Presentation transcript:

1 Conditional Logic October 6, 2017

2 New vocab!! Selection – A generic term for a type of programming statement that uses a Boolean condition to determine (i.e., select) whether or not to run a certain block of code. This generally, but not always, refers to an if statement. Know this for the AP!

3 Comparison Operators == “is equal to” != “is not equal to”
> “is (strictly) greater than” < “is (strictly) less than” >= “is greater than or equal to” <= “is less than or equal to” The AP exam will use > and <.

4 Comparison Operators (2 + 2) == 4 evaluates to TRUE.
"Joe Perry" == "joe perry" evaluates to FALSE (strings are case- sensitive). "Ringo" <= "Ringo" evaluates to TRUE. "TGOD" != "tgod" evaluates to TRUE. N.B. Some languages will allow you to compare a string to a number (e.g., "3" == 3). C++ will give you an error if you try to do this!

5 Comparing Strings Inequalities such as <= and >= can be used with strings—these perform a comparison in lexicographical order. In English: which would come first in a dictionary? Capital letters are "less than" lowercase letters since they have lower ASCII values. "A" < "a" "Dog" < "dog" "dog" < "dogwood"

6 Logical Operators && AND || OR ! NOT
This is the character above \ (itself above Enter) on the keyboard. ! NOT

7 Logical Operators TRUE && TRUE evaluates to TRUE.
TRUE && FALSE evaluates to FALSE. FALSE && TRUE evaluates to FALSE. FALSE && FALSE evaluates to FALSE.

8 Logical Operators TRUE || TRUE evaluates to TRUE.
TRUE || FALSE evaluates to TRUE. FALSE || TRUE evaluates to TRUE. FALSE || FALSE evaluates to FALSE.

9 Logical Operators !TRUE evaluates to FALSE. !FALSE evaluates to TRUE.

10 Comparison Operators N.B. Do NOT use a string of inequalities, such as the following: if(x < z < y) { cout << "z is between x and y.\n"; } The proper way to do this is with the "and" operator: if( (x < z) && (z < y) )

11 Logical Operators Consider the following code:
int cohen_age = 29; cohen_age >= 21 && cohen_age < 30; The bolded statement evaluates to TRUE.

12 Logical Operators Consider the following code:
int cohen_age = 29; cohen_age >= 21 && cohen_age < 25; The bolded statement evaluates to FALSE. The following would evaluate to TRUE: cohen_age >= 21 || cohen_age < 25;

13 Logical Operators Be careful with parentheses!
int cohen_age = 29; string day = "Friday"; !((cohen_age == 29) && !(day == "Friday")) The bolded code evaluates to TRUE. cohen_age == 29 is TRUE. day == "Friday" is TRUE—so !(day == "Friday") is FALSE. TRUE && FALSE evaluates to FALSE. !(FALSE) evaluates to TRUE.

14 Comparison Operators Generally, comparison operators are used as part of if statements: string band; cout << "What is your favorite band?\n"; cin >> band; if(band == "Aerosmith" || band == "aerosmith") { cout << "Good answer.\n"; } else cout << "Try again.\n";

15 Comparison Operators Generally, comparison operators are used as part of if statements: int age; cout << "What is your age?"; cin >> age; if(age >= 21 && age <= 29) { cout << "You're in the prime of your life.\n"; } else cout << "That's unfortunate.\n";

16 Logical Operators OR has a slightly different meaning in logic than in ordinary usage. Consider: “Is this elevator going up or down?” We would normally answer “up” or “down.” The logical answer is “yes”: either it is going up or it is going down.


Download ppt "Conditional Logic October 6, 2017."

Similar presentations


Ads by Google