In Lua, you can do this:
foo = a and b or c and d or e
Which is equivalent to (at least I am pretty sure it is equivalent to):
local foo = nil
if a then
foo = b
elseif c then
foo = d
else
foo = e
end
Is there anything equivalent or similar to this in C++?
There’s the ternary operator. It has funny precedence, so it’s good practice to always parenthesize it.
Note that this only works if
b,d, andecan reduce to the same type. Ifais adouble,dis afloatandeis anint, your result will always be cast to adouble.