The 'or' Operator
Part of the Fundamentals section of Coddy's Lua journey — lesson 21 of 90.
The logical <strong>or</strong> operator works differently. It returns true if at least one of the conditions is true, making it perfect for situations where you have multiple ways to satisfy a requirement.
Here's how the or operator works:
hasKey = true
hasPass = false
canOpenDoor = hasKey or hasPass
-- canOpenDoor is true because at least one condition is trueThe or operator follows this simple rule: if the first condition is true, the entire expression becomes true without checking the second condition. Only when both conditions are false does the result become false.
playerGold = 50
playerLevel = 15
requiredGold = 100
requiredLevel = 20
canEnterZone = playerGold >= requiredGold or playerLevel >= requiredLevel
-- canEnterZone is false because both conditions are false (50 < 100 AND 15 < 20)Challenge
EasyCheck if a player can access a secret area by verifying they have either a special key or a high enough clearance level. Create a variable called hasSpecialKey and assign it the value false. Create a variable called clearanceLevel and assign it the value 7. Create another variable called requiredClearance and assign it the value 5. Use the or operator to check if the player either has the special key or meets the clearance requirement, storing the result in a variable called canAccessArea. Print the boolean result.
Cheat sheet
The logical or operator returns true if at least one of the conditions is true:
hasKey = true
hasPass = false
canOpenDoor = hasKey or hasPass
-- canOpenDoor is true because at least one condition is trueThe or operator returns true if the first condition is true without checking the second condition. Only when both conditions are false does the result become false:
playerGold = 50
playerLevel = 15
requiredGold = 100
requiredLevel = 20
canEnterZone = playerGold >= requiredGold or playerLevel >= requiredLevel
-- canEnterZone is false because both conditions are falseTry it yourself
-- Create variables as specified in the challenge
-- TODO: Write your code here
-- Print the result
print(canAccessArea)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator