Module 4 · Making decisions
Using elseif
Order several checks so the first matching branch wins.
What you will learn
- Add an elseif branch
- Order overlapping conditions from most specific to broadest
- elseif
- A conditional branch checked only after earlier branches fail.
- order
- The sequence in which branches are evaluated.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest can describe a score as expert, ready, or still learning, but the highest category must be checked before a lower threshold catches it.
The mental model
Luau checks branches from top to bottom. It runs the first matching body and skips the rest of that chain.
When thresholds overlap, start with the highest or most specific requirement.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
local is read as “local”: it creates a name that is available only in the current code scope.
if is read as “if”: it starts a decision by evaluating a condition.
then is read as “then”: it marks the body that can run when the condition is truthy.
elseif is read as “else if”: it checks another condition only after earlier branches did not run.
else is read as “else”: it provides the fallback branch.
end is read as “end”: it closes a block such as a decision, loop, or function.
A tiny example
--!strictlocal score = 85if score >= 100 then print("Master explorer")elseif score >= 50 then print("Quest ready")else print("Keep collecting")endLine by line
- 3
score is the value being categorized.
- 5
The highest threshold is checked first.
- 7
elseif is reached only when the first check fails.
- 9
else handles scores below both thresholds.
- 11
One end closes the chain.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Begin at the first executable statement. Comments and blank lines do not run; a branch chooses which block runs, and a loop may revisit its body.
Line 3: score is the value being categorized.
Line 5: The highest threshold is checked first.
Line 7: elseif is reached only when the first check fails.
Line 9: else handles scores below both thresholds.
Line 11: One end closes the chain.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: Boundary tests enter the intended category and reveal why order matters.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
Which category is printed for a score of 85?
Try it
Mark each step after you have tried it.
Common mistake
if score >= 50 thenelseif score >= 100 thenA score of 100 also meets >= 50, so the later branch can never win. Put the narrower high threshold first.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- elseif extends one ordered decision.
- The first matching branch wins.
- Boundary tests expose ordering errors.
Next: Next, you will make combined conditions readable and safe.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.