Relational Operators
Prepared By Dr Goh Wan Inn
Relational Operators
• Used to compare numbers to determine relative
order
• Operators:
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
Slide 4- 3
Relational Expressions
• Boolean expressions – true or false
• Examples:
12 > 5 is true
7 <= 5 is false
if x is 10, then
x == 10 is true,
x != 8 is true, and
x == 8 is false
Slide 4- 4
Relational Expressions (cont..)
• Can be assigned to a variable:
result = x <= y;
• Assigns 0 for false, 1 for true
• Do not confuse = and ==
Slide 4- 5
Logical Operators
• Used to create relational expressions from other
relational expressions
• Operators, meaning, and explanation:
AND New relational expression is true if both expressions
are true
OR New relational expression is true if either expression is
true
NOT Reverses the value of an expression – true expression
becomes false, and false becomes true
Slide 4- 6
Logical Operators - examples
x = 12, y = 5, z = -4
(x > y) and (y > z) true
(x > y) and (z > y) false
(x <= z) or (y == z) false
(x <= z) or (y != z) true
not(x >= z) false
Slide 4- 7
The and Operator
Slide 4- 8
The or Operator
Slide 4- 9
The not Operator
Slide 4- 10
Logical Operators - notes
• not has highest precedence, followed by
and, then or
• If the value of an expression can be
determined by evaluating just the sub-
expression on left side of a logical operator,
then the sub-expression on the right side will
not be evaluated (short circuit evaluation)
Slide 4- 11