Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Operators Operator Meaning < Less than > Greater than

Similar presentations


Presentation on theme: "Relational Operators Operator Meaning < Less than > Greater than"— Presentation transcript:

1 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 ( == ).

2 Relational Expressions Numeric
Expression Meaning Value 4 <= less than or equal to 5 true(1) 4 != not equal to 4 false(0) 4.0 == 4 real 4.0 equals integer 4 true(1) 4.0 >= greater than or true(1) equal to 4.0 4.0 > greater than 5.0 false(0)

3 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)

4 Logical Operators The three logical operators allow for creating more complex comparisons. Operator Meaning Type of operator ! Not unary && and binary || or binary

5 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

6 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) ______

7 Operator Precedence Operator Precedence Function calls highest
! + - & (Unary operators) * / % + - < <= >= > == != && || = lowest Note: You may use parentheses to change the order of evaluation.


Download ppt "Relational Operators Operator Meaning < Less than > Greater than"

Similar presentations


Ads by Google