Lua - Relational Operators



Following table shows all the relational operators supported by Lua language. Assume variable A holds 10 and variable B holds 20 then −

Operator Description Example
== Checks if the value of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
~= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (A ~= B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Example - Equality and Non-Equality Checks

In this example, we're creating two variables a and b and using relational operators, we've performed equality and non-equality checks and printed the results −

main.lua

a = 10
b = 20

print("a == b = ", (a == b))
print("a ~= b = ", (a ~= b))

Output

When you build and execute the above program, it produces the following result −

a == b =    false
a ~= b =    true

Example - Greater than, Less than Checkss

In this example, we're creating two variables a and b and using relational operators, we've performed greater than and less thanchecks and printed the results −

main.lua

a = 10
b = 20

print("a > b = ", (a > b))
print("a < b = ", (a < b))

Output

When you build and execute the above program, it produces the following result −

a > b =    false
a < b =    true

Example - Greater than Or Equals, Less than Or Equals Checks

In this example, we're creating two variables a and b and using relational operators, we've performed greater than or equals to and less than or equals tochecks and printed the results −

main.lua

a = 10
b = 20

print("a >= b = ", (a >= b))
print("a <= b = ", (a <= b))

Output

When you build and execute the above program, it produces the following result −

a >= b =    false
a <= b =    true
lua_operators.htm
Advertisements