Skip to content
If/Then/Else
step 1/5

Reading — step 1 of 5

Learn

~1 min readControl Flow

Lua uses if/then/elseif/else/end. The then is required; end closes the block.

local age = 18
if age < 13 then
    print("child")
elseif age < 20 then
    print("teen")
else
    print("adult")
end

Comparisons return booleans: == ~= < > <= >=. Note ~= (not !=) for inequality.

Logical operators: and, or, not (NOT && || !). They short-circuit and return one of their operands (not necessarily a bool):

local name = user_name or "anonymous"  -- common idiom for defaults

Falsy values: only false and nil. Zero is truthy. Empty string is truthy. Watch out!

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…