Shop OBEX P1 Docs P2 Docs Learn Events
IF Statements — Parallax Forums

IF Statements

Joe FishbackJoe Fishback Posts: 99
edited 2005-01-10 03:18 in BASIC Stamp
I am using "IF" statements to compare the outputs of sensers and I am trying to understand·the "IF" statements. In looking at working code using "IF" statements, I find different syntex. One example is as follows:

··IF IN3· = 0 AND IN4 = 0 THEN u_turn
· IF IN3· = 0 THEN right_turn
· IF IN4· = 0 THEN left_turn

This goes to labels if true. Is this going to a label similiar to "GOTO"? Could a "GOSUB" be used after "THEN"? When I tried this sometimes I got an error message wanting "ELSE or ENDIF".

Other example:

· IF ir1 < 475· AND ir0 < 475 THEN
··· PULSOUT 13, 850: PULSOUT 12, 650
· ELSEIF ir1 < 475 AND ir0 > 475 THEN
··· PULSOUT 13, 650: PULSOUT 12, 850
· ELSEIF ir1 > 475 AND ir0 < 475 THEN
··· PULSOUT 13, 850: PULSOUT 12, 850
· ELSE
··· PULSOUT 13, 650: PULSOUT 12, 650
· ENDIF

Why the difference·? When do you use which?

Joe Fishback
·

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2005-01-10 00:55
    The 'IF' statement in PBasic has two major forms -- single line and multi-line.

    The single line form is: IF xxx THEN yyy ' Where yyy IS a 'goto' label.
    or IF xxx THEN GOSUB yyyy ' Where yyyy is a GOSUB label

    The multi-line is:
    IF xxx THEN
    yyy
    zzz
    ENDIF

    The multi-line form is indicated when the THEN is followed on the same line by nothing.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-10 03:18
    You multiple IF-THEN construct is best replaced with SELECT-CASE (see help file).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
Sign In or Register to comment.