Shop OBEX P1 Docs P2 Docs Learn Events
ENDIF,IF, whats the difference? — Parallax Forums

ENDIF,IF, whats the difference?

lightsoutlightsout Posts: 22
edited 2005-03-20 21:35 in BASIC Stamp
Still a newbe.whats the difference· in these examples ? whay use ENDIF·or ELSEIF ?

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

OR

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"
··· IF (value = 4000) THEN
····· DEBUG "Value was equal to 4000"
····IF· (value < 4000) THEN
····· DEBUG "Value was less than 4000"

··· DEBUG CR, CR
··· PAUSE 1000
· LOOP


I re-read and re-read and I still don't get it unless it is just a matter of good code writing. Thanks!

Post Edited (lightsout) : 3/19/2005 10:00:50 PM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-19 22:58
    The second example won't compile -- in PBASIC 2.0 the IF-THEN is expecting a label, and in 2.5 the IF-THEN is expecting and ENDIF.·

    To get to your question, though, it's a matter of style.· In this case you code is dealing with a single variable, so it makes sense to keep the logic in a single structure.· Another solution to this problem is:

    · SELECT value
    ··· CASE >4000
    ····· DEBUG "Value was greater than 4000"
    ··· CASE 4000
    ····· DEBUG "Value equal to 4000"
    ··· CASE ELSE
    ····· DEBUG "Value less than 4000"
    · ENDSELECT



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • lightsoutlightsout Posts: 22
    edited 2005-03-20 00:37
    Thanks again, Jon
    Your answer also brings the question,·Will all of my programs I have worked on in 2.0 work fine in 2.5 ??? Like if I copy & paste ?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-03-20 02:27
    ·· You can still use all your 2.0 programs under the newest BASIC Stamp software.· But to support the 2.5 commands, you will need the 2.5 directive in your programs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-20 05:59
    Like Chris says, your 2.0 programs should be fine in 2.5 -- the only thing you'll need to check for is illegal labels like:

    GOTO Loop

    'LOOP' is a 2.5 keyword. Other than that, you'll have no problems

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-03-20 06:31
    Jon,

    ·· Good point!· I wondered though (Since I haven't tried it), would specifying the 2.0 directive allow those types of things in the new software?· I suppose I could try it...But then what would be the point?· confused.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-20 16:13
    Yes, if you specify $PBASIC 2.0 all "old" programs will run without change. But ... as soon as you want to do something like IF-THEN-ELSE you must switch to 2.5 syntax rules and adjust any illegal labels as I pointed out above.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-03-20 21:35
    Well that should clear up any questions about backward compatibility.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.