The 'and' Operator
Part of the Fundamentals section of Coddy's Lua journey — lesson 20 of 90.
The logical and operator allows you to combine two boolean expressions, and it only returns true when both conditions are true.
Here's how the and operator works:
playerGold = 150
playerLevel = 8
requiredGold = 100
requiredLevel = 5
canBuyItem = playerGold >= requiredGold and playerLevel >= requiredLevel
-- canBuyItem is true because both conditions are true (150 >= 100 AND 8 >= 5)The and operator follows a simple rule: if the first condition is false, the entire expression becomes false without even checking the second condition. Both parts must be true for the whole expression to be true.
hasKey = true
hasPermission = false
canEnter = hasKey and hasPermission -- canEnter is false
-- Even though hasKey is true, hasPermission is false, so the result is falseChallenge
EasyCheck if a player can purchase a special weapon by verifying they have both enough gold and a high enough level. Create a variable called playerGold and assign it the value 250. Create a variable called playerLevel and assign it the value 8. Create two more variables for the requirements: requiredGold with the value 200 and requiredLevel with the value 10. Use the and operator to check if the player meets both requirements, storing the result in a variable called canBuyWeapon. Print the boolean result.
Cheat sheet
The logical and operator combines two boolean expressions and returns true only when both conditions are true:
playerGold = 150
playerLevel = 8
requiredGold = 100
requiredLevel = 5
canBuyItem = playerGold >= requiredGold and playerLevel >= requiredLevel
-- canBuyItem is true because both conditions are true (150 >= 100 AND 8 >= 5)If the first condition is false, the entire expression becomes false without checking the second condition:
hasKey = true
hasPermission = false
canEnter = hasKey and hasPermission -- canEnter is false
-- Even though hasKey is true, hasPermission is false, so the result is falseTry it yourself
-- Create variables for player stats
-- TODO: Write your code here
-- Check if player can buy weapon using 'and' operator
-- TODO: Write your code here
-- Print the result
print(canBuyWeapon)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