Updated April 11, 2023
Definition on Lua ‘and’ Operator
Lua ‘and’ operator is a logical operator that is used widely in the programs by providing the 2 operands and applying the operator between them. In Lua, ‘and’ operator works a little bit different in comparison to the other programming languages. It returns the value of either operand on evaluation as a result instead of displaying the boolean values ‘true’ or ‘false’. It returns ‘true’ if both the operands are holding the true values. In Lua, false and ‘nil’ are considered to be ‘false’ values otherwise everything is ‘true’. These logical operators are used to solve complex problems and take decisions accordingly by the conditional statements along with them.
Syntax:
Below given is the basic syntax of using the logical ‘and’ operator in Lua:
result = operand1 and operand2
- In Lua ‘result’ variable will hold either the value of ‘operand1’ or ‘operand2’ instead of the boolean true or false values depending on the value that variable holds.
How does an ‘and’ operator work in Lua?
In Lua language, there are basically 3 logical operators which are ‘and’, ‘or’ and ‘not’. These logical operators are quite useful to solve complex decision-making problems. In Lua, ‘and’ logical operator considers ‘nil’ and ‘false’ as false else all the other operands are considered to be true.
- Logical ‘and’ returns the first operand if that operand is false (either false or nil).
- Logical ‘and’ returns the second argument if the first operand is true.
- In Lua, logical and returns one of the operators as a result instead of the boolean values ‘true’ or ‘false’.
An important point to note regarding the ‘and’ operator in Lua is that it follows the shortcut strategy for evaluation of the statement. Second operand is evaluated only when necessary. Otherwise maximum decisions are taken by viewing the value of the first operand.
Below given is the basic table showing the output based on the value of the variables by applying the ‘and’ operator on them to help you in the better understanding:
S.No. | Statement | Result |
1. | print(345 and 654) | 654 |
2. | print( nil and 546) | nil |
3. | print (465 and nil) | nil |
4. | print (false and 454) | false |
5. | print (456 and false) | false |
Examples
Below given are some of the examples showing the usage and the working of logical and operator in a Lua program:
Example #1
Using the ‘and’ operator to check the range of variable using if-else statement
Code:
number_to_test = 9800
-- checking whether the number lies within the range or not using and operator
if number_to_test > 9000 and number_to_test < 10000 then
print("Congratulations!!!, '" .. number_to_test .. "', is valid and within the range..")
else
print("Sorry!! the entered number is not within the range..")
end
Output:
Explanation:
In the above code, we have taken a variable ‘number_to_test’ which is holding a value 9800. Conditional statements if and else are applied on that variable to test whether the value of the variable lies between 9000 and 10000 using the ‘and’ logical operator. As both the conditions of the ‘and’ operator evaluates to be true, statements inside the ‘if’ block are executed and printed on the console.
Example #2
Using the ‘and’ operator with both true operand values in if- else statement
Code:
num1 = 45
num2 = 54
-- both the operands are neither ‘nil’ nor ‘false’, so the 'and' becomes true
if ( num1 and num2 )
then
print("Result of the above statement is true" )
else
print("Result of above statement is false")
end
Output:
Explanation:
In the above code, two variables, ‘num1’ and ‘num2’ are taken holding the values 45 and 54 respectively. Logical ‘and’ operator is applied on both of the variables in the conditional if- else statement. As both the variables are evaluated to be true (containing neither ‘false’ nor ‘nil’ value), so the code under the ‘if’ statement is executed and printed on the console.
Example #3
Checking the results of ‘and’ operator with different operand values
Code:
num1 = 0
num2 = 54
num3 = nil
-- 'and' returns second argument if the result is true
print( num1 and num2 )
-- 'and' returns the first argument if it is nil (evaluates to be false in Lua)
print (num3 and num2)
-- 'and' returns the first argument if it is false
print (false and num1)
Output:
Explanation:
In the above code, 3 variables are taken i.e. num1, num2, and num3 holding the values 0, 54, and ‘nil’ respectively. As we know that ‘and’ operator in Lua does not return the boolean true or false values instead it returns one of the operands depending on the result of the evaluation of the statement. Logical ‘and’ of operands ‘num1’ and ‘num2’, i.e. 0 and 54 results in the ‘true’ which ultimately returns the second operand, i,e. 54 in Lua.
Similarly, for the logical ‘and’ of num3 and num2, i.e. ‘nil’ and 54 respectively will result in the ‘nil’, because if the first operand is ‘false’, it will return the first operand if that operand is ‘false’.
For the logical ‘and’ of false and ‘num1’, i.e. false and 0 results in the false on evaluation. For the same reason mentioned above.
Example #4
Checking the result of ‘and’ operator with ‘false’ operand value in if-else statement
Code:
num1 = false
num2 = 54
-- one of the operands is ‘false’, so the result of 'and' becomes false
if ( num1 and num2 )
then
print("Result of the above statement is true" )
else
print("Result of above statement is false")
end
Output:
Explanation:
In the above code, 2 variables num1 and num2 are taken holding the values ‘false’ and 54 respectively. Conditional statements if-else is used and the logical ‘and’ operator is applied on them. Since num1 and num2, i.e. false and 54 will evaluate to be false, so the code/ statements inside the else block would be executed and printed on the console.
Conclusion
Above description clearly explains how the logical ‘and’ operator works in Lua. Logical operators are basically used to mix two conditional statements and execute the specific piece of code on the basis of their results. Though learning various operators is the basic thing in any programming language, it is important for the programmer to understand them properly in order to use them for complex situations.
Recommended Articles
We hope that this EDUCBA information on “Lua and” was beneficial to you. You can view EDUCBA’s recommended articles for more information.