Relational Operators Operator Meaning < Less than > Greater than == Equal to <= Less than or equal to >= Greater than or equal to != Not equal to These are binary operators There must be expressions on either side of the operator Those expressions should evaluate to comparable types. The comparisons will evaluate to true (1) or false (0) Caution The two-character relational operators must appear exactly as written above, with the two characters in the proper order and without intervening spaces. Don’t confuse the assignment operator ( = ) with the relational operator ( == ).
Relational Expressions Numeric Expression Meaning Value 4 <= 5 4 less than or equal to 5 true(1) 4 != 4 4 not equal to 4 false(0) 4.0 == 4 real 4.0 equals integer 4 true(1) 4.0 >= 4.0 4.0 greater than or true(1) equal to 4.0 4.0 > 5.0 4.0 greater than 5.0 false(0)
Relational Expressions ASCII Characters Expression Meaning Value ‘B’ > ‘A’ ‘B’ greater than ‘A’ true(1) ‘g’ < ‘h’ ‘g’ less than ‘h’ true (1) ‘1’ < ‘2’ ‘1’ less than ‘2’ true(1) ‘B’ == ‘b’ ‘B’ equal to ‘b’ false(0) ‘B’ != ‘b’ ‘B’ not equal to ‘b’ true(1) ‘B’ > ‘a’ ‘B’ greater than ‘a’ false(0) ‘B’ > ‘1’ ‘B’ greater than ‘1’ true(1) ‘?’ > ‘!’ ‘?’ greater than ‘!’ true(1)
Logical Operators The three logical operators allow for creating more complex comparisons. Operator Meaning Type of operator ! Not unary && and binary || or binary
Logical Truth Tables p !p NOT truth tables true false false true Semantics of boolean expressions can be described by truth tables (where p and q represent boolean expressions) p !p NOT truth tables true false false true p q p && q AND truth table true true true true false false false true false false false false p q p || q _ OR truth table true false true false true true
Understanding Boolean Logic Suppose that char ans = ‘Y’; int sum = 23; int index = 17; double r = 7.5; int done = true; Evaluate (ans == ‘Y’) && (index != 0) ______ !(ans < ‘z’) ______ (sum == 45) || (index < r) ______ ( !done) && (index < 30) ______ done && (sum > 21) ______ (r <= r) || (!done) ______ (sum <= 40) || done && !(done) ______
Operator Precedence Operator Precedence Function calls highest ! + - & (Unary operators) * / % + - < <= >= > == != && || = lowest Note: You may use parentheses to change the order of evaluation.