Lua - Logical Operators
Following table shows all the logical operators supported by Lua language. Assume variable A holds true and variable B holds false then −
| Operator | Description | Example |
|---|---|---|
| and | Called Logical AND operator. If both the operands are non zero then condition becomes true. | (A and B) is false. |
| or | Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. | (A or B) is true. |
| not | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A and B) is true. |
Example - Logical AND Operation
In this example, we're creating two variables a and b and using logical operator we've performed a logical AND operation and printed the result −
main.lua
a = true
b = false
print("a and b = ", (a and b))
Output
When you build and execute the above program, it produces the following result −
a and b = false
Example - Logical OR Operation
In this example, we're creating two variables a and b and using logical operator we've performed a logical OR operation and printed the result −
main.lua
a = true
b = false
print("a or b = ", (a or b))
Output
When you build and execute the above program, it produces the following result −
a or b = true
Example - Logical NOT Operation
In this example, we're creating two variables a and b and using logical operator we've performed a logical NOT on logical OR operation and printed the result −
main.lua
a = true
b = false
print("not(a or b) = ", not(a or b))
Output
When you build and execute the above program, it produces the following result −
not(a or b) = false