0% found this document useful (0 votes)
8 views10 pages

Relational and Logical Operators Guide

Uploaded by

nazihah jafardin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Relational and Logical Operators Guide

Uploaded by

nazihah jafardin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like