Tutorial: Relational Operators

This tutorial will introduce you to relational operators.

What is a Relational Operator?

Relational operators are used in boolean expressions to check the relation between two things.

In other words, relational operators help make descisons based on answering these questions :

  • Is number a equal to number b?
  • Is number a bigger than number b?

Equal to

=, used as a relational operator, means equal to.

If 2 = 2 Then Print "2 is equal to 2. Duh."
If 2 = 4 Then Print "This  won't be displayed."

Sync
WaitKey

You can also use variables and math on either side of =.

myVar = 2

If 4 = myVar + 2 Then Print "4 does equal 2 + 2."

Sync
WaitKey

Greater Than and Less Than

>, greater than, < and less than be used to check if a number is greater than or less than another number.

In the example below, run it as-is, then change the value of the health variable to 0 and see what happens.

health = 100

If health > 0 Then "The player object is still alive."
If health < 1 Then "The player loses a life."

Sync
WaitKey

Greater Than or Equal to and Less Than or Equal To

>= means greater than or equal to.
<= means less than or equal to.

Both of them combine the the above operators with the equality operator.

In the example below, try changing the x# variable below to a negative value, or a value above 600.

leftSideScreen = 0
rightSideScreen = 600

x# = 0

If x# <= leftSideScreen Then Print "This position is off the left side of the screen."
If x# >= rightSideScreen Then Print "This position is off the right side of the screen."

Sync
WaitKey

Not Equal to

<> means not equal to

secretCode = 42

If secretCode = 1234 Then Print "The secret code is 1234. Good job."
If secretCode <> 1234 Then Print "Wrong code in the 'secretCode' variable."

Sync
WaitKey

Next Steps

Categories: Beginner Tutorials : Programming Tutorials : Tutorials