← Back to course
easy ✦ 75 Studs

If, Elseif, Else

Pick a path with if/elseif/else, and combine conditions with and, or, not.

This is where code stops being a straight line and starts making choices. An if block runs only when its condition is true. Add elseif to check a backup condition, and else to catch everything that’s left. Lua reads them top to bottom and stops at the first branch that’s true, so the order you write them in actually matters.

local stamina = 40
if stamina > 75 then
  print("Sprint!")
elseif stamina > 25 then
  print("Jog.")
else
  print("Catch your breath.")
end

Two things to keep tidy: every if needs a matching then, and the whole block closes with one end. Forget the end and Lua will tell you it expected one, which is a friendly nudge, not a disaster.

You can also stack conditions. and is true only when both sides are true. or is true when either side is. not flips a boolean. Think of and as a strict bouncer (everyone must pass) and or as a generous one (anyone gets in).

Now make it yours: grade the score with the A/B/C/F ladder. Remember to check 90+ first. Once it works, add a secret “S” rank for 100.

Output
Click Run to see your output.