This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ETHANATOR360's profile picture

Conditional operator in lua

Started by ETHANATOR360, 22 November 2012 - 08:41 AM
ETHANATOR360 #1
Posted 22 November 2012 - 09:41 AM
is their a conditional operator in lua just like java?
Kingdaro #2
Posted 22 November 2012 - 09:44 AM
If you mean comparisons in ifs:

== | is equal to
> | is greater than
< | is less than
>= | is greater than or equal to
<= | is less than or equal to
~= | is not equal to

However if you mean something like

var = (condition) ? onTrue : onFalse;
then in lua, it's

var = condition and onTrue or onFalse
Tiin57 #3
Posted 22 November 2012 - 09:46 AM
Just to add; I don't believe there is:

x++
x+=y
x--
x-=y
ETHANATOR360 #4
Posted 22 November 2012 - 12:17 PM
i mean like x = y ? 1 : 0
Kingdaro #5
Posted 22 November 2012 - 12:32 PM
then in lua, it's

var = condition and onTrue or onFalse
Watcher7 #6
Posted 22 November 2012 - 12:41 PM
i mean like x = y ? 1 : 0

That's called the ternary operator.
http://codepad.org/CEtRWl6l

Kingdaro also showed you this.

EDIT: Isn't this shit supposed to alert me when someone posts something new? First the staff and recent thread frames are broken, now this.