Question

I need to evaluate a condition like:

condition1 and (condition2 or condition3)

where condition2 and condition3 should be evaluated only if condition1 is true. How do I write this in lua?

Is next code correct?

if condition1 and (condition2 or condition3) then
...
end
Was it helpful?

Solution

From Lua manual:

Both and and or use short-cut evaluation; that is, the second operand is evaluated only if necessary.

So in the expression condition1 and (condition2 or condition3), condition2 or condition3 is only evaluated if condition1 is true.

Note that this also means in the expression condition2 or condition3, condition3 is only evaluated if condition2 is false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top