Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a shortened "if then" syntax available in Lua?

Tags:

lua

Is there a shorted "if / then" syntax available in Lua (I'm using Corona SDK specifically), like in some other languages... In particular along the lines of:

 res = (a == b) ? "It worked" : "It did NOT work"
like image 497
Greg Avatar asked Dec 07 '22 14:12

Greg


1 Answers

To represent x = a ? b : c, the lua-users wiki suggests:

A frequently used and highly recommend solution is to combine the and and or binary operators in a way that closely approximates the ternary operator:

x = a and b or c

To represent x = a ? b : c ? d : e, the wiki further suggests:

x = a and b or c and d or e

WARNING: This technique may fail if b or d are ever nil or false.

like image 95
Dan D. Avatar answered Jan 11 '23 22:01

Dan D.