Shop OBEX P1 Docs P2 Docs Learn Events
If then, else if, then ... — Parallax Forums

If then, else if, then ...

hyperlinearhyperlinear Posts: 4
edited 2013-12-11 13:27 in BASIC Stamp
Every time I try to use this command, the syntax checker ALWAYS says "Expected A Label" right after the first THEN ... ???? Below is the code example as given in the help section of the editor and IT DOESN'T WORK EITHER. What gives here? I'm using a BS2p stamp. (Pardon me if this has been addressed somewhere else, as the search terms "IF", "THEN", "ELSEIF", etc. seem "too common" words for the forum search engine.)

value VAR Word


Main:
DO
PULSIN 0, 1, value ' measure pulse input
DEBUG DEC value, CR
IF (value > 4000) THEN ' evaluate duration
DEBUG "Value was greater than 4000"
ELSEIF (value = 4000) THEN
DEBUG "Value was equal to 4000"
ELSE
DEBUG "Value was less than 4000"
ENDIF
DEBUG CR, CR
PAUSE 1000
LOOP

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-11 09:50
    The code tokenizes just fine for me. Of course, the difference I see is I automatically added the Stamp Model and PBASIC Version tags to my copy. Without specifying 2.5 the code defaults to 2.0 which doesn't support conditionals in that format/construct.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    value VAR Word
    
    
    
    
    Main:
    DO
    PULSIN 0, 1, value ' measure pulse input
    DEBUG DEC value, CR
    IF (value > 4000) THEN ' evaluate duration
    DEBUG "Value was greater than 4000"
    ELSEIF (value = 4000) THEN
    DEBUG "Value was equal to 4000"
    ELSE
    DEBUG "Value was less than 4000"
    ENDIF
    DEBUG CR, CR
    PAUSE 1000
    LOOP
    
  • davejamesdavejames Posts: 4,047
    edited 2013-12-11 09:52
    Hello hyperlinear,

    According to the Stamp documentation, this command is supported in PBasic 2.5.

    Have you declared that at the top of your code?
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    


    ***************************************
    ...rats! Mr. Savage beat me to it!!!
  • hyperlinearhyperlinear Posts: 4
    edited 2013-12-11 10:04
    Always grabbed stamp directive and coded ... for us that don't RTFM, perhaps a note somewhere that you have to tell the editor what version to use? I always kept old versions of the editor if I wanted backwards compatibility. DUH ...

    Thanks!
  • GenetixGenetix Posts: 1,749
    edited 2013-12-11 10:40
    The compiler defaults to PBASIC 2.0 unless you add a PBASIC 2.5 directive to your program.

    In PBASIC 2.0:
    IF (condition) THEN label ' The program will execute the code starting at label if the condition is met.

    Adding this line will stop that error:
    ' {PBASIC 2.5} ' This is a compiler directive telling the compiler to use PBASIC 2.5
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-11 12:46
    Always grabbed stamp directive and coded ... for us that don't RTFM, perhaps a note somewhere that you have to tell the editor what version to use? I always kept old versions of the editor if I wanted backwards compatibility. DUH ...

    Thanks!

    Well, I'm not sure about that requirement...I mean any code examples we have that require that directive to be there should have it in the example code itself. So if you're copying the code from something it should match the source exactly or this can happen.
  • davejamesdavejames Posts: 4,047
    edited 2013-12-11 13:27
    ....require that directive to be there should have it in the example code itself.

    Well Sir, it depends at which "example" one is looking.

    There are what I call the "snippets" of examples on the main discussion page of any command, and then there are the example pages.

    The main discussion page presumes (not "assumes" :innocent:) the information at the top of the page has been read and understood, so the snippets then make sense. The full example pages have all required code.

    It appears that the OP copied the snippet only.
Sign In or Register to comment.