Control Flow Details
Logical Guards
Lua combines conditions with and, or, and not.
combined condition
A guard can require multiple facts before choosing the success branch.
Logical Guards
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)
points ← 7, active ← true, status ← (empty)
1local points→ 7 = 7 --@points=3, 122local active→ true = true3local status→ (empty) = ""status ← wait
6 status = "bonus"7else8 status→ wait = "wait"9endprint("points=" .. points)
11print("points=" .. points7)12print("active=" .. tostring(activetrue))13print("status=" .. statuswait)outputpoints=7 active=true status=wait
points ← 3, active ← true, status ← (empty)
1local points→ 3 = 32local active→ true = true3local status→ (empty) = ""status ← wait
6 status = "bonus"7else8 status→ wait = "wait"9endprint("points=" .. points)
11print("points=" .. points3)12print("active=" .. tostring(activetrue))13print("status=" .. statuswait)outputpoints=3 active=true status=wait
points ← 12, active ← true, status ← (empty)
1local points→ 12 = 122local active→ true = true3local status→ (empty) = ""status ← bonus
5if activetrue and points12 >= 10 then6 status→ bonus = "bonus"7elseprint("points=" .. points)
11print("points=" .. points12)12print("active=" .. tostring(activetrue))13print("status=" .. statusbonus)outputpoints=12 active=true status=bonus
Check Both Facts
pointsstarts at7.activeistrue.- The guard needs both
activeandpoints >= 10. 7 >= 10is false, sostatusbecomeswait. | 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