IF Statements
Joe Fishback
Posts: 99
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
·
··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
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 Williams
Applications Engineer, Parallax
Dallas, TX· USA