Ternary operator

Control flow / Conditional statements / if/else statements

local n = -42

-- cond and a or b: no ternary operator in Lua
local classify =
  n > 0 and "positive" or "negative"
-- classify = negative

-- Trap: a is false or nil, so b is chosen anyway
local answer = false
local wrong = n < 0 and answer or "other"
-- wrong = other

-- When a can be false or nil, use if
local right
if n < 0 then
  right = answer
else
  right = "other"
end
-- right = false

print("classify = " .. classify)
print("wrong = " .. wrong)
print("right = " .. tostring(right))