if Statement
if / elseif / else
if condition then
# body
elseif otherCondition then
# body
else
# body
end
The then keyword is required after each condition. The block is closed with end. The elseif and else clauses are optional, and any number of elseif clauses may appear.
elseif vs else if
These are both valid but semantically different.
elseif (one word) is part of a single if statement and requires one end:
if score >= 90 then
grade = "A"
elseif score >= 80 then
grade = "B"
else
grade = "F"
end
else if (two words) creates a nested if inside an else block. Each if requires its own end:
if score >= 90 then
grade = "A"
else
if score >= 80 then
grade = "B"
else
grade = "F"
end # closes inner if
end # closes outer if
Use elseif for straightforward conditional chains. Use else if only when you need to declare variables or execute statements in the else block before the nested condition.