whenever cond == true as well as the condition after the and before the or, the second value (0) is passed instead of the first, and when the entire condition on the left hand equates to false:
local var = (cond == true and 0) or str -- the brackets is one condition in the end, comprising of two evaluations
then the or keyword just does its job, the value (the string) on the other end is passed.
You can basically eliminate if statements and even run functions:
local n = 4
local function f() return n == 4 and "n = 4" or n end
print(cond and n < 3 or f())
-- code in original post
local Condition = false
local Variable
if Condition then
Variable = "A"
else
Variable = "B"
end
print(Variable)
-- can be converted to:
Variable = Condition and "A" or "B"
This probably doesn’t have any considerable benefit but I tend to not use if statements within my coding structures:
s.Source = (notDefinedAsServices and source or "") .. (
table.find(definedLabels, selectedClassLabel) and ""
or
not table.insert(definedLabels, selectedClassLabel)
and totalDef
) .. (
not notDefinedAsServices and source or ""
)
Though using if s can simplify and make your code look more organized.