Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   FIRST Tech Challenge (http://www.chiefdelphi.com/forums/forumdisplay.php?f=146)
-   -   [FTC]: Robot C (http://www.chiefdelphi.com/forums/showthread.php?t=78636)

JohnFogarty 10-11-2009 21:36

Re: [FTC]: Robot C
 
Quote:

Originally Posted by alphadog0309 (Post 882035)
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...

Yeah the programming Gods like you to do that it's really just unessesary code, and yeah it's been a long day i know i forgot the loop XD.

JohnFogarty 10-11-2009 21:37

Re: [FTC]: Robot C
 
Quote:

Originally Posted by John_1102 (Post 882050)
Yeah the programming Gods like you to do that it's really just unessesary code, and yeah it's been a long day i know i forgot the loop XD.

if(joy1Btn(1) )
{
code for what you want to happen here
}
else
{
code to make that stop happening
}

this is still as efficiant i tried it today.

l0jec 10-11-2009 22:04

Re: [FTC]: Robot C
 
Quote:

Originally Posted by alphadog0309 (Post 882035)
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:
Code:

if(value)
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. ;)

alphadog0309 11-11-2009 18:18

Re: [FTC]: Robot C
 
ahh i see... yeah that makes sense... i guess my brain is just wired in some screwed up way haha

JohnFogarty 11-11-2009 19:27

Re: [FTC]: Robot C
 
Quote:

Originally Posted by l0jec (Post 882054)
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:
Code:

if(value)
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. ;)

Tis exactly right :D


All times are GMT -5. The time now is 13:05.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi