Relational Operators
Part of the Fundamentals section of Coddy's Lua journey — lesson 19 of 90.
Relational operators that let you compare the size or magnitude of numbers. These operators help you determine which value is larger, smaller, or if they're equal in size.
The four relational operators are:
playerLevel = 15
requiredLevel = 10
isHighEnough = playerLevel > requiredLevel -- true (15 is greater than 10)
isTooLow = playerLevel < requiredLevel -- false (15 is not less than 10)
meetsMinimum = playerLevel >= requiredLevel -- true (15 is greater than or equal to 10)
belowMaximum = playerLevel <= 20 -- true (15 is less than or equal to 20)Just like equality operators, relational operators always return a boolean value - either true or false. The >= and <= operators are particularly useful because they include the possibility of equality, making them perfect for checking minimum or maximum requirements.
Challenge
EasyCheck if a player meets the level requirement to enter a dangerous dungeon zone. Create a variable called playerLevel and assign it the value 12. Create another variable called requiredLevel and assign it the value 15. Use the greater than or equal to operator to check if the player's level meets the minimum requirement, storing the result in a variable called canEnter. Print the boolean result.
Cheat sheet
Relational operators compare the size or magnitude of numbers and return boolean values (true or false):
>- greater than<- less than>=- greater than or equal to<=- less than or equal to
playerLevel = 15
requiredLevel = 10
isHighEnough = playerLevel > requiredLevel -- true (15 is greater than 10)
isTooLow = playerLevel < requiredLevel -- false (15 is not less than 10)
meetsMinimum = playerLevel >= requiredLevel -- true (15 is greater than or equal to 10)
belowMaximum = playerLevel <= 20 -- true (15 is less than or equal to 20)Try it yourself
-- Create variables for player level and required level
-- TODO: Write your code here
-- Check if player can enter and print the result
-- TODO: Write your code hereThis 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