Сложные условия

Поток управления / Условные операторы / Операторы 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 относится ко всему условию в ( )
if not (a >= b or a >= c) then
  notes[#notes + 1] = "a is the smallest"
end

-- ~= истинно, когда значения различаются
if a ~= b and b ~= c then
  notes[#notes + 1] = "all three differ"
end

-- Цепочки сравнений нет: пишем обе части
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

-- Ловушка: a < b < c - это (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)