复杂条件

控制流 / 条件语句 / if/else 语句

local a, b, c = 3, 5, 7

local notes = {}
if c >= a and c >= b then
  notes[#notes + 1] = "nothing is larger than c"
end

-- not applies to the whole condition in ( )
if not (a >= b or a >= c) then
  notes[#notes + 1] = "a is the smallest"
end

-- ~= is true when the two values differ
if a ~= b and b ~= c then
  notes[#notes + 1] = "all three differ"
end

-- No chained comparison: write both parts
if a < b and b < c then
  notes[#notes + 1] = "b is between a and c"
end

for _, note in ipairs(notes) do
  print(note)
end

-- Trap: a < b < c is (a < b) < c
local ok, err = pcall(function()
  return a < b < c
end)
-- ok = false
-- err = main.lua:29: attempt to compare
-- boolean with number
print("ok = " .. tostring(ok))
print("err = " .. err)