Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline conditions in Lua (a == b ? "yes" : "no")?

Is there anyway to use inline conditions in Lua?

Such as:

print("blah: " .. (a == true ? "blah" : "nahblah"))
like image 234
Softnux Avatar asked Oct 15 '22 06:10

Softnux


People also ask

How do you say not equal to in Lua?

3.2 – Relational Operators The operator == tests for equality; the operator ~= is the negation of equality. We can apply both operators to any two values. If the values have different types, Lua considers them different values. Otherwise, Lua compares them according to their types.

Does Lua have ternary operator?

Even though Lua lacks ternary operators explicitly, there are ways to closely approximate it, as described below.

Which of the following is ternary operator?

Remarks. The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows: The first operand is implicitly converted to bool .


1 Answers

Sure:

print("blah: " .. (a and "blah" or "nahblah"))
like image 176
John Zwinck Avatar answered Oct 16 '22 20:10

John Zwinck