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

an if statement is a variable

Started by darkmists, 06 April 2013 - 05:20 AM
darkmists #1
Posted 06 April 2013 - 07:20 AM
i want to know if it is possible to put an if statement into a variable? if so how?

example of what i want to do

x = if i < 1 then
rs.setOutput("side", true)
end
PixelToast #2
Posted 06 April 2013 - 07:22 AM
rs.setOutput("side", i <1)

not sure what you want though
Lyqyd #3
Posted 06 April 2013 - 07:22 AM
No. Unless you're trying to use a function? What is the use case here, what are you actually trying to accomplish?

Edit:
rs.setOutput("side", i <1)

not sure what you want though

This always sets the output, rather than only setting the output if i is less than one.
Engineer #4
Posted 06 April 2013 - 07:29 AM
You can use the ternary operator

local x = z==1 and 'its ture' or 'its false'
PixelToast #5
Posted 06 April 2013 - 07:33 AM
This always sets the output, rather than only setting the output if i is less than one.
.-.
rs.setOutput("side", i <1 or rs.getOutput("side"))
that better?
darkmists #6
Posted 06 April 2013 - 07:40 AM
nvm i figured it out while thinking of a way to explain. the code i want. very sorry about this. it was my stupid for forgetting how the code works.
remiX #7
Posted 06 April 2013 - 07:41 AM
rs.setOutput( "side", i < 1 and true or false )
PixelToast #8
Posted 06 April 2013 - 08:23 AM
rs.setOutput( "side", i < 1 and true or false )
"and true"
dafuq?
"or false"
i dont even

seriously these do nothing .-.
remiX #9
Posted 06 April 2013 - 09:26 AM
Lol ternary operator… What do you mean it doesn't do anything? I thought just plain
rs.setOutput( 'back', i < 1 )
would work but…
rs.setOutput("side", i <1) not sure what you want though
This always sets the output, rather than only setting the output if i is less than one.

EDIT:
Oh misunderstood lol, but setting it to false doesn't matter so rs.setOutput('side', i < 1) is fine really