Quote:
Originally Posted by alphadog0309
i dont know why but i was told and ive seen it done only this way in robotc but it think its supposed to be
if(joy1Btn(1) !=0)
{
action
}
don't know why i just do it like that...
|
What you're doing is perfectly valid, but may not be the "cleanest" way.
You need to understand that a boolean true/false in ROBOTC is the same as 1 or 0 where
'true' is the same as '1' and 'false' is the same as '0'. Therefore the follow statements are all logically equivalent:
Code:
if(value == true)
if(value == 1)
if(value != false)
if(value != 0)
Since the if statement is conditional on the truth of the argument, you can format the test for truth a number of ways. Understand that the "value == true" argument is going to be evaluated as either true ('value' is true(1)) or false ('value' is false(0)) and that is what the if() statement actually looks at.
So as long as the value variable in our example here is already a boolean, there is no need to explicitly check it for truth. That is there is no need to explicitly test if true is equal to true of if false is equal to false... just pass the boolean itself to the if() statement like:
Where does this tie into checking if a button is pressed using the joy1Btn() function?
Well because those functions return a boolean. That means when you call the function you get back either a true or a false and there is no need to see if that is not equal to 0. Just test against the returned boolean like:
Code:
if(joy1Btn(1))
{
//do stuff
}
Now changing
if(joy1Btn(1) != 0) to
if(joy1Btn(1)) will not make your code behave any differently, but it will make it much more readable. As someone much wiser once said, any fool can write code that a computer can understand, but a good programmer writes code that a human can understand.
Hope this clears us some of the fog.
