Conditional Logic October 6, 2017
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!
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 <.
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!
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"
Logical Operators && AND || OR ! NOT This is the character above \ (itself above Enter) on the keyboard. ! NOT
Logical Operators TRUE && TRUE evaluates to TRUE. TRUE && FALSE evaluates to FALSE. FALSE && TRUE evaluates to FALSE. FALSE && FALSE evaluates to FALSE.
Logical Operators TRUE || TRUE evaluates to TRUE. TRUE || FALSE evaluates to TRUE. FALSE || TRUE evaluates to TRUE. FALSE || FALSE evaluates to FALSE.
Logical Operators !TRUE evaluates to FALSE. !FALSE evaluates to TRUE.
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) )
Logical Operators Consider the following code: int cohen_age = 29; cohen_age >= 21 && cohen_age < 30; The bolded statement evaluates to TRUE.
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;
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.
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";
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";
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.