Shop OBEX P1 Docs P2 Docs Learn Events
simple Syntax question IF THEN ELSE — Parallax Forums

simple Syntax question IF THEN ELSE

tochustochus Posts: 10
edited 2004-12-03 19:49 in BASIC Stamp
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
·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-02 04:46
    You were close -- just drop the curly braces.


    IF (distance > 10) AND (IRdetect = 0) THEN
      GOSUB Forward
      counter = counter + 1
      ballFound = 1
    ENDIF
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • tochustochus Posts: 10
    edited 2004-12-02 04:51
    what is the difference between elseif and if?
    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.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-02 05:00
    With ELSEIF you can create compound decision structures -- be careful though, you can create problems for yourself it they get to unwieldy.·

    IF (condition1) THEN
      ' do something #1
    ELSEIF (condition2) THEN
      ' do something #2
    ELSE
      ' do something else
    ENDIF
    


    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
  • tochustochus Posts: 10
    edited 2004-12-03 05:16
    can i write it in this way? I wonder if the second if statement process the following three statement?

    thanks for reply



    IF ( ballgotten = 1) THEN
    ····· ballgotten = 0
    ······· IF ( changedirection = 1) THEN
    ······· GOSUB right
    ······· changedirection = 0
    ······· counter = counter + 1
    ······· ENDIF
    ····· ENDIF
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-12-03 15:49
    Yes, your nested 'IF' should work correctly -- and I find it MUCH easier to follow than that 'ELSEIF' construct.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-03 15:55
    Yes, that will work. I also agree with Allan and tend not to use ELSEIF as things can become a bit unwieldy if you're not careful.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-12-03 19:49
    ELSEIF is a straightforward way to deal with a cascade of conditions, when a SELECT-CASE statement is not quite suitable. You have to remember that ELSEIF is in fact part of the ELSE clause, executed when the original condition is not true.

    IF condxA THEN
      statementsA
    ELSEIF condxB THEN
      statementsB
    ELSEIF condxC THEN
      statementsC
    ELSE
      statememtsD
    ENDIF
    



    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:

    IF condxA THEN
      statementsA
    ELSE
      IF condxB THEN
        statementsB
      ELSE
        IF condxC THEN
          statementsC
        ELSE
          statememtsD
        ENDIF
      ENDIF
    ENDIF
    



    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. yeah.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.