16. if / elseif / else — Homework solutions
The .lua solution files are in
exercises/16/homework/solutions/.
Problem 1 — Even or odd
Problem. Read a number; print even or
odd.
How to think about it. n % 2 is
0 for even and 1 for odd (negatives give
-1 or 1, but == 0 still works).
One if/else.
Worked solution.
io.write("Enter a whole number: ")
local n = tonumber(io.read())
if n % 2 == 0 then
print("even")
else
print("odd")
endCommon mistakes.
- Writing
if n % 2 = 0. Single=is assignment; the check needs==. - Forgetting
tonumber.n % 2on a string is an error.
Problem 2 — Roblox level gate
Problem. Different messages depending on which of two requirements the player fails.
How to think about it. Two booleans give four
combinations, but only three are bad. Spell each out with
elseif:
- both ok → enter
- level low, key ok → "Level too low."
- level ok, key missing → "Missing the key."
- both wrong → "Level too low and missing the key."
Branch order matters: handle the most specific failures first.
Worked solution.
local level = 12
local has_key = true
if level >= 10 and has_key then
print("You can enter the dungeon.")
elseif level < 10 and not has_key then
print("Level too low and missing the key.")
elseif level < 10 then
print("Level too low.")
else
print("Missing the key.")
endThe combined failure is checked before the single ones. If
elseif level < 10 came first, it would swallow the
combined case, so that message never shows.
Common mistakes.
- Using
&&or||. Lua usesandandor. - Forgetting that
not has_keymeans the key is missing. Say it out loud: "not has key".
Problem 3 — Grade letter
Problem. Standard A/B/C/D/F based on cutoffs.
How to think about it. Each branch checks the
highest cutoff not yet caught. With elseif, once a branch
matches, the rest are skipped.
Worked solution.
io.write("Score (0-100): ")
local score = tonumber(io.read())
if score >= 90 then
print("Grade: A")
elseif score >= 80 then
print("Grade: B")
elseif score >= 70 then
print("Grade: C")
elseif score >= 60 then
print("Grade: D")
else
print("Grade: F")
endCommon mistakes.
- Listing the conditions from low to high. Putting
score >= 60first catches every passing score, including 95, and gives them all a D. - Using
>instead of>=. A score of exactly 90 should be an A, not a B.
Challenge — Largest of three
Problem. Three numbers, no math.max, no
loops. Print the largest.
How to think about it. Compare them pair by pair.
a >= b and a >= c means a
is the largest, or tied for it. >= (not
>) handles ties: if a == b, the first
branch wins and prints a.
Worked solution.
local a = 7
local b = 12
local c = 12
if a >= b and a >= c then
print(a)
elseif b >= c then
print(b)
else
print(c)
endTrace through a = 7, b = 12,
c = 12:
a >= b?7 >= 12is false. First branch skipped.b >= c?12 >= 12is true. Prints12.
For a = 12, b = 12, c = 5:
a >= b?12 >= 12is true.a >= c?12 >= 5is true.- Both true, so print
a,12. Done.
Common mistakes.
- Using
>instead of>=. With three-way ties, no branch matches and the program falls through toprint(c)even whenaheld the max. The value is right, the logic muddled.
Done?
The next chapter, Boolean logic in depth, digs into
what and, or, and not actually
do. After that come loops — the other way to make a
program do something different each run.