Relational and Boolean Operators

From PZwiki
Jump to: navigation, search
Language policy Language: English

Relational Operators

Relational operators are used to compare two values and return a Boolean (true or false) as a result. They consist of:

  • > More than
  • < Less than
  • == Equal to
  • >= More than or equal to
  • <= Less than or equal to
  • ~= Not equal to

Notice that when comparing values we use two equal signs ==, rather than when we are assigning a variable, when we use just one.

They are fairly self-explanatory when comparing numbers:

print(1 > 2)
print(1 < 2)
print(1 >= 1)
print(5 <= 10)
print(1 == 2)
print(1 ~= 1)

We can also use the to compare strings, although this is slightly more complicated. For single character strings, it compares the collation (the order it appears in that locale):

print("a" < "b")
print("a" == "a")
print("c" >= "d")
print("A" < "a")
print("A" < "B")
print("Z" > "a")

For multiple line strings, it compares the first character that is different:

print("abcd" < "abce")
print("abc" < "abcd")
print("ab" > "Abcdefghijk")

Note that == and ~= can be used to compare values of any datatype:

print(false == nil)
print(false ~= 0)
print(1 == "1")

Notice that two different datatypes will never be considered equal. The rest of the relational operators can only be used when comparing two strings or two numbers, otherwise Lua will throw up an error.

Boolean Operators

This consists of three operators: and, or and not.

and

The and operator returns true if both it's operands (the values used by an operator) are true, otherwise it returns false. Have a look at these examples to get a better understanding:

print(true and true)
print(true and false)
print(1 < 2 and "a" > "A")
print(false and nil)
print(nil and false)
print(true and 10)
print(10 and true)

Notice that if the first operand is false or nil, then the first operand is returned, otherwise the second operand is returned.

or

The or operand returns true if one of it's operands is true, otherwise it returns false:

print(1 > 1 or 1 == 1)
print(1 ~= 1 or nil)
print(nil or false)
print(true or false)
print(1 < 2 or 1 ~= 2)

In contrast to and, or returns it's first operand if it is something other than false or nil, otherwise it returns the second operand. This can be useful when writing a function that uses variables that may be uninitialised, for example:

print(FavouriteFilm or "Die Hard")
FavouriteFilm = "Pan's Labyrinth"
print(FavouriteFilm or "Die Hard")

As FavouriteFilm == nil, the or operator returned the string instead. When we assigned it a value, being the first operand it was returned first.

not

not is the only unary operator on our list, as it only takes one operand. The rest are considered binary, as they take two operands. When using not, the operand goes to the right. If the operand is false or nil, the result is true, otherwise it returns false:

print(not false)
print(not nil)
print(not 0)
print(not "Noooo")
print(not (1 < 2))

Notice that I used brackets around the relational operator in the last example. This is because not has high precedence than <, resulting in 'false < 2', which throws up an error.

Updated Precedence

This means the precedence list from the last tutorial needs to be updated:

^ (Eponentiation, right associative)
not, # (length operator, explained next tutorial)
* (Multiplication), / (Division)
+ (Addition), - (Subtract)
.. (String concatenation, explained next tutorial)
<, >, ~=, ==, >=, <= (Relational operators)
and
or

Don't worry about memorising this list, it is just a useful way to introduce the different operators you will most commonly use.

Summary

  • The Relational Operators are: <, >, ~=, ==, >=, <=
  • These can be used to compare two values
  • and operator returns true if both its operands are true
  • or operator returns true if one of its operands are true
  • not returns true if its operand is false or nil