short form if then

 How do i write this as one line? The attempt below throws an error.

— original

  if num > 0 then

    r = num-inc

  else

    r = num+inc

  end

— attempted short form

r = (num > 0) ? (num-inc) : (num+inc)

Anything obviously missing?

Hi Matthew,

You could actually take your code and just put it in one line like this:

[lua]if num>0 then r=num-inc else r=num+inc end[/lua]

Or you could write it in a slightly shorter form like this:

[lua]r = (num>0) and num-inc or num+inc[/lua]

  • Andrew

That was exactly what I was looking for! Thanks Andrew. 

Hi Matthew,

You could actually take your code and just put it in one line like this:

[lua]if num>0 then r=num-inc else r=num+inc end[/lua]

Or you could write it in a slightly shorter form like this:

[lua]r = (num>0) and num-inc or num+inc[/lua]

  • Andrew

That was exactly what I was looking for! Thanks Andrew.