The elseif Statement
Part of the Fundamentals section of Coddy's Lua journey — lesson 42 of 90.
The elseif statement allows you to check multiple conditions in sequence. This extends the basic if-then-else structure to handle complex decision-making scenarios.
Here's the syntax for using elseif:
if condition1 then
-- code for first condition
elseif condition2 then
-- code for second condition
elseif condition3 then
-- code for third condition
else
-- code if none of the conditions are true
endLua evaluates each condition from top to bottom. When it finds the first condition that is true, it executes that block of code and skips all remaining conditions. If none of the conditions are true, the else block runs (if present).
Here's a practical example with grade evaluation:
local grade = 85
if grade >= 90 then
print("Excellent")
elseif grade >= 70 then
print("Good")
else
print("Needs Improvement")
endSince the grade is 85, the first condition (85 >= 90) is false, but the second condition (85 >= 70) is true, so "Good" will be printed.
Challenge
EasystudentGrade and assign it the value 75. Use an if-elseif-else statement to check the grade and print the appropriate message:- If the grade is 85 or above, print
"Excellent" - If the grade is 65 or above (but less than 85), print
"Good" - For all other grades, print
"Needs Improvement"
Cheat sheet
The elseif statement allows you to check multiple conditions in sequence:
if condition1 then
-- code for first condition
elseif condition2 then
-- code for second condition
elseif condition3 then
-- code for third condition
else
-- code if none of the conditions are true
endLua evaluates each condition from top to bottom. When it finds the first condition that is true, it executes that block of code and skips all remaining conditions.
local grade = 85
if grade >= 90 then
print("Excellent")
elseif grade >= 70 then
print("Good")
else
print("Needs Improvement")
endTry it yourself
-- Declare the studentGrade variable
local studentGrade = 75
-- TODO: Write your code here
-- Use if-elseif-else statement to check the grade and print the appropriate message
-- If studentGrade is 85 or above, print "Excellent"
-- If studentGrade is 65 or above, print "Good"
-- Otherwise, print "Needs Improvement"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