Updated April 19, 2023
Introduction to Lua or
The Lua or operator is a logical operator who connects more than two conditions and declares the true expression. The logical function is to declare the Boolean value of the two or more than two operands in the source code. It is a symbol to show the Boolean true or one value in more than one Lua code expression. It shows the true Boolean value when at least one operand is true or non zero. Code shows more than two expressions are non-zero or at least one expression is none zero then the output becomes true called Lua or operator. It is a type of logical condition that shows true output even one operand is true otherwise shows false when all operands are zero.
Syntax:
Given below is the syntax:
first_variable or second_variable
first_variable or second_variable;
Explanation:
- The syntax used “or” expression in the source code.
- It work either using a semicolon or without using a semicolon at the end of the statement.
- Either the first variable or second variable is true then return the true.
- Both variables are true or non zero then return the true.
- Both variables are false or zero then return the false.
How or Operator Works in Lua?
Given below shows how or operator works:
Step 1: Lua programming IDE Environmental Setup.
The Lua text editor, Lua compiler, and Lua interpreter install in your computer as per the operating system and software version.
Or
If you do not have the software, you can use Lua Online IDEs to code and start Lua programming.
Step 2: Create the Lua File.
The file creates with the .lua extension and writes a source code.
File name: Luacomment.lua
Step 3: Use operator syntax in the source code.
We can create a function with two arguments.
function luaOr(luaor1 , luaor2)
end
Use the operator syntax with a variable.
Luaoperator = luaor1 or luaor2
Return the output of the operator source code.
luaOr(1, 0)
Combine the operator demo example to understand the working procedure.
File name: Luacomment.lua
function luaOr(luaor1 , luaor2)
Luaoperator = luaor1 or luaor2
print("Lua or operator value:", Luaoperator)
end
luaOr(1, 0)
Examples
Given below are the examples mentioned:
Example #1
With zero or one conditions example and output.
Code:
function luaOr(luaor1 , luaor2)
Luaoperator = luaor1 or luaor2
print(" The Lua or operator value after working :", Luaoperator)
print("Lua first operand :", luaor1)
print("Lua second operand :", luaor2)
end
luaOr( )
luaOr( 1, 0 )
luaOr( 0, 1 )
luaOr( 0, 0 )
luaOr( 1, 1 )
luaOr(1, 0)
Output:
Explanation:
- There are four conditions output display.
- It is using zero (0) or non zero (1) condition.
Example #2
With true or false conditions example and output.
Code:
function luaOr(luaor1 , luaor2)
Luaoperator = luaor1 or luaor2
print(" The Lua or operator value after working :", Luaoperator)
print("Lua first operand :", luaor1)
print("Lua second operand :", luaor2)
end
luaOr( )
luaOr( true, false )
luaOr( false, true )
luaOr( true, true )
luaOr( false, false )
Output:
Explanation:
- There are four boolean conditions output display.
- It is using false (0) or true (1) conditions.
Example #3
With combination of conditions example and output.
Code:
function luaOr(luaor1 , luaor2)
Luaoperator = luaor1 or luaor2
print(" The Lua or operator value after working :", Luaoperator)
print("Lua first operand :", luaor1)
print("Lua second operand :", luaor2)
end
luaOr( )
luaOr( true, 0 )
luaOr( false, 1 )
luaOr( 1, true )
luaOr( 0, false )
luaOr( false, 0 )
luaOr( true, 0 )
Output:
Explanation:
- It combines the digit or word Boolean condition in the source code.
- If the first operand is in digit format then output display in 0 or 1 format.
- If the first operand is in word format then output display in the true or false format.
Example #4
With more than two operand example and output.
Code:
function luaOr(luaor1 , luaor2, luaor3, luaor4 )
Luaoperator1 = luaor1 or luaor2
Luaoperator2 = luaor3 or luaor4
Luaoperator3 = luaor1 or luaor4 or luaor2
Luaoperator4 = luaor1 or luaor2 or luaor3 or luaor4
print(" The Lua or operator (Luaoperator1) :", Luaoperator1)
print(" The Lua or operator (Luaoperator2) :", Luaoperator2)
print(" The Lua or operator (Luaoperator3) :", Luaoperator3)
print(" The Lua or operator (Luaoperator4) :", Luaoperator4)
end
print( "print first condition:" )
luaOr( 0, 4, 1, 2 )
print( "print second condition:" )
luaOr( 3, 1, 0, 2 )
print( "print third condition:" )
luaOr( 0, 0, 0, 1 )
print( "print fourth condition:" )
luaOr( 4, 1, 8, 1 )
Output:
Explanation:
- We can see in the output there are print different values as per or condition.
- It displays the first operand value as an output in the display window.
- There is no sorting between the large or small value of the operand.
Example #5
With other logical operator example and output.
Code:
function luaOr(luaor1 , luaor2)
Luaoperator = not (luaor1 or luaor2 )
print(" The Lua or operator value after working :", Luaoperator)
Luaoperator1 = (luaor1 or luaor2 )
print(" The Lua or operator value after working :", Luaoperator1)
Luaoperator2 = (luaor1 or luaor2 ) and luaor2
print(" The Lua or operator value after working :", Luaoperator2)
end
luaOr( 1, 0 )
luaOr( 0, 0 )
luaOr( 1, 1 )
Output:
Conclusion
The operator is useful to show the probability of the Boolean value. The operator helps to make it easy to declare the value of the operand. It makes easy, readable, and sorted source code.
Recommended Articles
We hope that this EDUCBA information on “Lua or” was beneficial to you. You can view EDUCBA’s recommended articles for more information.