Lua combines conditions with and, or, and not.

combined condition A guard can require multiple facts before choosing the success branch.

Logical Guards

points
logical_guards.lua
Replay: real traced execution (multi-file project)
local points = 7
local active = true
local status = ""

if active and points >= 10 then
  status = "bonus"
else
  status = "wait"
end

print("points=" .. points)
print("active=" .. tostring(active))
print("status=" .. status)
local points = 3
local active = true
local status = ""

if active and points >= 10 then
  status = "bonus"
else
  status = "wait"
end

print("points=" .. points)
print("active=" .. tostring(active))
print("status=" .. status)
local points = 12
local active = true
local status = ""

if active and points >= 10 then
  status = "bonus"
else
  status = "wait"
end

print("points=" .. points)
print("active=" .. tostring(active))
print("status=" .. status)
  1. points ← 7, active ← true, status ← (empty)

    1local points→ 7 = 7 --@points=3, 122local active→ true = true3local status→ (empty) = ""
  2. status ← wait

    6  status = "bonus"7else8  status→ wait = "wait"9end
  3. print("points=" .. points)

    11print("points=" .. points7)12print("active=" .. tostring(activetrue))13print("status=" .. statuswait)
    outputpoints=7
    active=true
    status=wait
  1. points ← 3, active ← true, status ← (empty)

    1local points→ 3 = 32local active→ true = true3local status→ (empty) = ""
  2. status ← wait

    6  status = "bonus"7else8  status→ wait = "wait"9end
  3. print("points=" .. points)

    11print("points=" .. points3)12print("active=" .. tostring(activetrue))13print("status=" .. statuswait)
    outputpoints=3
    active=true
    status=wait
  1. points ← 12, active ← true, status ← (empty)

    1local points→ 12 = 122local active→ true = true3local status→ (empty) = ""
  2. status ← bonus

    5if activetrue and points12 >= 10 then6  status→ bonus = "bonus"7else
  3. print("points=" .. points)

    11print("points=" .. points12)12print("active=" .. tostring(activetrue))13print("status=" .. statusbonus)
    outputpoints=12
    active=true
    status=bonus

Check Both Facts

  1. points starts at 7.
  2. active is true.
  3. The guard needs both active and points >= 10.
  4. 7 >= 10 is false, so status becomes wait. | Points | Active | Status | | --- | --- | --- | | 3 | true | wait | | 7 | true | wait | | 12 | true | bonus |

Exercise: logical_guards.lua

Use and to require active status and enough points before printing bonus