Comparison

Comparison operators are used to compare two variables, typically numbers.

Operators

The Lua core is capable of the following comparison operators:

Less than: < Compares the left variable to the right variable. Returns true if the left is less than the right, otherwise returns false.

Greater than: > Compares the left variable to the right variable. Returns true if the left is greater than the right, otherwise returns false

Less than or equals: <= Compares the left variable to the right variable. Returns false if the left is greater than the right, otherwise returns true. This is the logical opposite of the greater than operator.

Greater than or equals: >= Compares the left variable to the right variable. Returns false if the left is less than the right, otherwise returns true. This is the logical opposite of the less than operator.

Equals: == Compares the left variable to the right variable. Returns true if the two variables are equal, or false if they are not.

Not Equals: ~= Compares the left variable to the right variable. Returns false if the two variables are equal (a few exceptions, see special cases), or true if they are not. This is the logical opposite of the equals operator.

Special cases

  • NaN (Not a Number) is the only Lua value that is not equal to itself.
  • Table equality is by reference, not by value. This means that {3} == {3} is false because the two tables have different references/are at different locations in memory, even though they contain the same values. However, local tbl = {3}; print(tbl == tbl) is true as the reference is the same and they are considered the same table.
  • Comparison can be overriden using metamethods.
gollark: Are you ASSUMING what my algorithm is INTENDING TO DO?
gollark: More zip implies more good.
gollark: ```pythonfor (i, x), (j, y) in zip(enumerate(a), enumerate(a)): if x < y: a[i], a[j] = y, x```
gollark: It does at least have fixed-width instructions.
gollark: ARM's quite CISCy now too.
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.