Operator Precedence
Part of the Fundamentals section of Coddy's Lua journey — lesson 16 of 90.
When you write mathematical expressions with multiple operators, Lua follows specific rules to determine which operations to perform first. This concept is called operator precedence, and it works just like the order of operations you learned in mathematics.
In Lua, multiplication and division are performed before addition and subtraction:
result = 2 + 3 * 4 -- result is 14, not 20
-- Lua calculates 3 * 4 first (12), then adds 2
result = 10 - 6 / 2 -- result is 7, not 2
-- Lua calculates 6 / 2 first (3), then subtracts from 10To control the order of operations, you can use parentheses (). Operations inside parentheses are always performed first:
result = (2 + 3) * 4 -- result is 20
-- Lua calculates 2 + 3 first (5), then multiplies by 4
result = (10 - 6) / 2 -- result is 2
-- Lua calculates 10 - 6 first (4), then divides by 2Challenge
EasyCalculate the average of three test scores using proper operator precedence. Create three variables for the test scores, then write a single expression that correctly calculates their average.
Create the following variables:
score1with the value85score2with the value92score3with the value78
Write a single expression to calculate the average by adding all three scores and dividing by 3. Use parentheses to ensure the addition happens before the division. Store the result in a variable called average and print it.
Cheat sheet
Lua follows operator precedence rules where multiplication and division are performed before addition and subtraction:
result = 2 + 3 * 4 -- result is 14, not 20
result = 10 - 6 / 2 -- result is 7, not 2Use parentheses () to control the order of operations:
result = (2 + 3) * 4 -- result is 20
result = (10 - 6) / 2 -- result is 2Try it yourself
-- Create the test score variables
local score1 = 85
local score2 = 92
local score3 = 78
-- TODO: Write your code here to calculate the average using proper operator precedence
-- Print the result
print(average)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