Quantcast
Viewing latest article 3
Browse Latest Browse All 4

Answer by Paul Kulchenko for Lua, if statement idiom

Is there a way to print "0 0"?

No, because and or expression always returns one result and the results you see are probably not for the reason you think they are.

local c,d = (a~=nil) and 1,1 or 0,0 -- prints "1 1"

This is calculated as ((a~=nil) and 1),(1 or 0),0. The first expression returns 1, the second (1 or 0) returns 1 and the last one is ignored (as you have two variables on the left side and three expressions on the right).

local c,d = (a==nil) and 1,1 or 0,0 -- prints "false 1"

This is calculated in a similar way, except (a==nil) is false and that's why you get the second result.

To do what you want, you'll need to split it into two expressions: one for c and one for d.


Viewing latest article 3
Browse Latest Browse All 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>