logical expressions Flashcards
Logical Expressions
- Used to ‘logically’ compare
letters, numbers, symbols, variables, constants, etc.
When a logical expression is ‘evaluated’ the answer
will either be TRUE or FALSE
Logical expressions are surrounded
by parenthesis ()
A logical expression should be read
like a question
Logical expressions are used in
Selection and Repetition statements
Simple Logical Operators
Used in a single logical expression
!=
(Inequality)
SIGN**
> (Greater than)
>= (Greater than or equal to)
< (Less than)
<= (Less than or equal to)
Complex Logical Operators
Used for combining one or more logical expressions)
&& (Logical AND) - This key is found above the ‘7’ key when holding the ‘Shift’ key
|| (Logical OR) - This key is found above the ‘Enter’ key when holding the ‘Shift’ key
Examples: (9 > 5) ? (x <= 20) ? (a < b + c) ? (grade != 'F') ? (name == "Mark") ? (gpa > 2.5) ?
( (score <= 0) && (score <= 100) ) ? ( (temp < 32) || (temp > 95) ) ?
int x = 15; int a = 5, b = 6, c = 7; char grade = 'F'; string name = "Joe"; double gpa = 3.8; int score = 81; double temp = 61; bool result = 0;
result = (9 > 5); cout << "1) " << result << endl; result = (x <= 20); cout << "2) " << result << endl; result = (a < b + c); cout << "3) " << result << endl; result = (grade != 'F'); cout << "4) " << result << endl; result = (name == "Mark"); cout << "5) " << result << endl; result = (gpa > 2.5); cout << "6) " << result << endl; result = (score <= 0 && score <= 100); cout << "7) " << result << endl; result = (temp < 32 || temp > 95); cout << "8) " << result << endl;