simple Syntax question IF THEN ELSE
tochus
Posts: 10
hello i have a simple question about pbasic using if then else statement.
i learned C ++ about if then else, they can use a ( ) including the statement, i wonder if the pbasic can do this.
also, if i want to compare on statement then execute three or more action what can i do. for instance,
IF distance > 10 and IRdetect = 0·THEN
{
·GOSUB FORWARD
·COUNTER = COUNTER + 1
·BALLFOUND = 1
}
ENDIF
SOMETHING LIKE THIS, HOW CAN I DO THIS.
THANKS FOR REPLYING
·
i learned C ++ about if then else, they can use a ( ) including the statement, i wonder if the pbasic can do this.
also, if i want to compare on statement then execute three or more action what can i do. for instance,
IF distance > 10 and IRdetect = 0·THEN
{
·GOSUB FORWARD
·COUNTER = COUNTER + 1
·BALLFOUND = 1
}
ENDIF
SOMETHING LIKE THIS, HOW CAN I DO THIS.
THANKS FOR REPLYING
·
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
i have wrote the following code
····· ELSEIF (counter =< 8 AND ultdis > 50 ) THEN············· ' check if the robot turn 360
····· GOSUB right
····· DEBUG ? counter·· ,CR·································· 'debug = printscreen
····· counter = counter + 1
····· ELSEIF (counter > 8 AND ultdis > 50 ) THEN··············· ' check if the robot turn 360
····· GOSUB forward
i wonder if the first elseif statement execute the following three statement. thanks so much for your reply
i really appreciate it.
If condition1 evaluates as true, then "do something #1" will execute, otherwise condition2 will be evaluated.· If condition2 is true, then "do something #2" will run, otherwise "do something else" will run.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
thanks for reply
IF ( ballgotten = 1) THEN
····· ballgotten = 0
······· IF ( changedirection = 1) THEN
······· GOSUB right
······· changedirection = 0
······· counter = counter + 1
······· ENDIF
····· ENDIF
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
The ELSEIF clause is only executed if condxA is false. Then, if condxB is true, none of the other following ELSEIF or ELSE clauses will be executed and it drops right through to the ENDIF.
The ELSEIF could be written as nested IFs, but in my opinion it is more prone to error and confusion:
I think that comes out the same?
I have a web page that explores how the compiler translates from PBASIC 2.5 back to the more primitive PBASIC 2.0. The old PBASIC (for you lucky newcomers!) had only the statement,
IF condx THEN label
The URL is:
www.emesystems.com/BS2pbasic25.htm
I have found that it often helps resolve confusion, to see how the translation is implemented under the hood in a few example programs. The form "IF condx THEN label" is never ambiguous! Spagetti, yes, ambiguous, no.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com