PBASIC syntax question (IF THEN)
Archiver
Posts: 46,084
In the new PBASIC, I don't think there is ELSEIF.
Thus, is this allowable?
1. IF IN0=0 THEN
'statement
ELSE
IF IN1=0 THEN
'statement
ELSE
'statement
ENDIF
ENDIF
2. Is there a limit to how many times I can nest a IF THEN ELSE
inside a ELSE (above example has 1 nested)?
3. Is there a better way of doing ELSEIF?
Thank you.
Thus, is this allowable?
1. IF IN0=0 THEN
'statement
ELSE
IF IN1=0 THEN
'statement
ELSE
'statement
ENDIF
ENDIF
2. Is there a limit to how many times I can nest a IF THEN ELSE
inside a ELSE (above example has 1 nested)?
3. Is there a better way of doing ELSEIF?
Thank you.
Comments
opening the help file to find out....
That said, you have a logic flaw in your code below. Why? Because an
input pin can have one of two states, yet your logic below accommodates
three conditions.
For demonstration, here's where one might use ELSEIF:
IF (temp < 68) THEN
CoolerCtrl = IsOff
HeaterCtrl = IsOn
ELSEIF (temp > 78) THEN
HeaterCtrl = IsOff
AcCtrl = IsOn
ELSE
HeaterCtrl = IsOff
AcCtrl = IsOff
ENDIF
This can get a bit messy (when testing a single variable), so
SELECT-CASE might provide cleaner coding:
SELECT temp
CASE < 68
CoolerCtrl = IsOff
HeaterCtrl = IsOn
CASE > 78
CoolerCtrl = IsOn
HeaterCtrl = IsOff
ELSE
CoolerCtrl = IsOff
HeaterCtrl = IsOff
ENDSELECT
Suggestion: When testing a single variable for complex conditions, use
SELECT-CASE. When two or more variables are part of the test,
IF-THEN-ELSE is the proper choice.
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
Original Message
From: basicstampede [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=fifOufTM5Cq6suXSlQ-e4FwwjcIGgway0g7AuW9naD0OuanVaGp_yeMV4Vb1sbcwEXloDjX6TCMhE3pWtEs1Org]basicstampede@y...[/url
Sent: Monday, March 29, 2004 8:34 AM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] PBASIC syntax question (IF THEN)
In the new PBASIC, I don't think there is ELSEIF.
Thus, is this allowable?
1. IF IN0=0 THEN
'statement
ELSE
IF IN1=0 THEN
'statement
ELSE
'statement
ENDIF
ENDIF
2. Is there a limit to how many times I can nest a IF THEN ELSE
inside a ELSE (above example has 1 nested)?
3. Is there a better way of doing ELSEIF?
Thank you.
Your specific example could be coded as,
IF IN0=0 THEN
'statement1
ELSEIF IN1=0 THEN 'statement2
ELSE
'statement3
ENDIF
In this case, statement 1 is executed if in0=0, regardless of the
state of in1. If in1=1, and in2=0, then statement2 is executed, and
finally, statement3 is executed iff both in0=1 and in1=1. Is that
your intent?
With regard to your second question, I don't think there is any limit
for nesting the IF-THEN-ELSEIF-ELSE-ENDIF constructs. PBASIC under
the hood resolves those to simple IF-THEN-GOTO statements. See
<http://www.emesystems.com/BS2pbasic25.htm> for more exploration of
that issue.
The logic above is kind of hard to follow, because the conditions are
spread out over several statements. Jon mentioned SELECT-CASE which
can make the logic crystal clear. Here is another option:
ON inA&%11 GOTO statement1, statement1, statement2, statement3
That is more clear in my mind because the 4 possible input states,
00, 01, 10 and 11, and the actions that follow are laid out in one
structure. It results in smaller faster code than the IF-THEN, too.
Under the PBASIC hood, the ON--GOTO is resolved simply to:
BRANCH inA&%11, [noparse][[/noparse] statement1, statement1, statement2, statement3 ]
-- regards,
Tracy
>In the new PBASIC, I don't think there is ELSEIF.
>Thus, is this allowable?
>
>1. IF IN0=0 THEN
> 'statement
> ELSE
> IF IN1=0 THEN
> 'statement
> ELSE
> 'statement
> ENDIF
> ENDIF
>
>2. Is there a limit to how many times I can nest a IF THEN ELSE
>inside a ELSE (above example has 1 nested)?
>
>3. Is there a better way of doing ELSEIF?
"If in0=1, and in1=0, then statement2 is executed".
> IF IN0=0 THEN
> 'statement1
> ELSEIF IN1=0 THEN 'statement2
> ELSE
> 'statement3
> ENDIF
>
>In this case, statement 1 is executed if in0=0, regardless of the
>state of in1. If in1=1, and in2=0, then statement2 is executed, and
>finally, statement3 is executed iff both in0=1 and in1=1. Is that
>your intent?
Actually, I could not find ELSEIF in the help section. I use editor
2.0 and 2.1, and neither has ELSEIF in the help section. Where are
you finding it?
Thank you.
--- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
wrote:
> You are not correct; ELSEIF is available in PBASIC 2.5. It only
takes
> opening the help file to find out....
>
> That said, you have a logic flaw in your code below. Why? Because
an
> input pin can have one of two states, yet your logic below
accommodates
> three conditions.
>
> For demonstration, here's where one might use ELSEIF:
>
> IF (temp < 68) THEN
> CoolerCtrl = IsOff
> HeaterCtrl = IsOn
> ELSEIF (temp > 78) THEN
> HeaterCtrl = IsOff
> AcCtrl = IsOn
> ELSE
> HeaterCtrl = IsOff
> AcCtrl = IsOff
> ENDIF
>
> This can get a bit messy (when testing a single variable), so
> SELECT-CASE might provide cleaner coding:
>
> SELECT temp
> CASE < 68
> CoolerCtrl = IsOff
> HeaterCtrl = IsOn
> CASE > 78
> CoolerCtrl = IsOn
> HeaterCtrl = IsOff
> ELSE
> CoolerCtrl = IsOff
> HeaterCtrl = IsOff
> ENDSELECT
>
> Suggestion: When testing a single variable for complex conditions,
use
> SELECT-CASE. When two or more variables are part of the test,
> IF-THEN-ELSE is the proper choice.
>
> -- Jon Williams
> -- Applications Engineer, Parallax
> -- Dallas Office
>
>
>
Original Message
> From: basicstampede [noparse][[/noparse]mailto:basicstampede@y...]
> Sent: Monday, March 29, 2004 8:34 AM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] PBASIC syntax question (IF THEN)
>
>
> In the new PBASIC, I don't think there is ELSEIF.
> Thus, is this allowable?
>
> 1. IF IN0=0 THEN
> 'statement
> ELSE
> IF IN1=0 THEN
> 'statement
> ELSE
> 'statement
> ENDIF
> ENDIF
>
> 2. Is there a limit to how many times I can nest a IF THEN ELSE
> inside a ELSE (above example has 1 nested)?
>
> 3. Is there a better way of doing ELSEIF?
>
> Thank you.
IF-THEN-ELSE explanation as it is part of that construct. I will updat
the Help file so that it's easier to find.
-- Jon Williams
-- Parallax
Original Message
From: basicstampede [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=u_alm0fELch6QIlap8QQI84eUGsQC_HGGRCOIq9iizVQyp5mf4GVzKKo66beESpzHqtbjXeVjzL5HJc_Wj6Rew]basicstampede@y...[/url
Sent: Monday, March 29, 2004 1:35 PM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] Re: PBASIC syntax question (IF THEN)
Jon, thanks for the info.
Actually, I could not find ELSEIF in the help section. I use editor
2.0 and 2.1, and neither has ELSEIF in the help section. Where are
you finding it?
Thank you.
--- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
wrote:
> You are not correct; ELSEIF is available in PBASIC 2.5. It only
takes
> opening the help file to find out....
>
> That said, you have a logic flaw in your code below. Why? Because
an
> input pin can have one of two states, yet your logic below
accommodates
> three conditions.
>
> For demonstration, here's where one might use ELSEIF:
>
> IF (temp < 68) THEN
> CoolerCtrl = IsOff
> HeaterCtrl = IsOn
> ELSEIF (temp > 78) THEN
> HeaterCtrl = IsOff
> AcCtrl = IsOn
> ELSE
> HeaterCtrl = IsOff
> AcCtrl = IsOff
> ENDIF
>
> This can get a bit messy (when testing a single variable), so
> SELECT-CASE might provide cleaner coding:
>
> SELECT temp
> CASE < 68
> CoolerCtrl = IsOff
> HeaterCtrl = IsOn
> CASE > 78
> CoolerCtrl = IsOn
> HeaterCtrl = IsOff
> ELSE
> CoolerCtrl = IsOff
> HeaterCtrl = IsOff
> ENDSELECT
>
> Suggestion: When testing a single variable for complex conditions,
use
> SELECT-CASE. When two or more variables are part of the test,
> IF-THEN-ELSE is the proper choice.
>
> -- Jon Williams
> -- Applications Engineer, Parallax
> -- Dallas Office
>
>
>
Original Message
> From: basicstampede [noparse][[/noparse]mailto:basicstampede@y...]
> Sent: Monday, March 29, 2004 8:34 AM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] PBASIC syntax question (IF THEN)
>
>
> In the new PBASIC, I don't think there is ELSEIF.
> Thus, is this allowable?
>
> 1. IF IN0=0 THEN
> 'statement
> ELSE
> IF IN1=0 THEN
> 'statement
> ELSE
> 'statement
> ENDIF
> ENDIF
>
> 2. Is there a limit to how many times I can nest a IF THEN ELSE
> inside a ELSE (above example has 1 nested)?
>
> 3. Is there a better way of doing ELSEIF?
>
> Thank you.
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject
and Body of the message will be ignored.
Yahoo! Groups Links
This message has been scanned by WebShield. Please report SPAM to
abuse@p....