453 lines
22 KiB
HTML
453 lines
22 KiB
HTML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
|
|
<head><title>IF…THEN</title>
|
|
<link href="../../SkinSupport/MadCap.css" rel="stylesheet" />
|
|
<link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
|
|
<script src="../../SkinSupport/MadCapAll.js" type="text/javascript">
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<h1 class="code">IF…THEN</h1>
|
|
<div class="ImagePlusCaption">
|
|
<div class="Col2">
|
|
<p>
|
|
<img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" />
|
|
<img src="../../graphics/pgm_icon2.gif" border="0" alt="BS2 icon" title="BS2 icon" />
|
|
<img src="../../graphics/pgm_icon2e.gif" border="0" alt="BS2e icon" title="BS2e icon" />
|
|
<img src="../../graphics/pgm_icon2sx.gif" border="0" alt="BS2sx icon" title="BS2sx icon" />
|
|
<img src="../../graphics/pgm_icon2p.gif" border="0" alt="BS2p icon" title="BS2p icon" />
|
|
<img src="../../graphics/pgm_icon2pe.gif" border="0" alt="BS2pe icon" title="BS2pe icon" />
|
|
<img src="../../graphics/pgm_icon2px.gif" border="0" alt="BS2px icon" title="BS2px icon" /><span class="code_in_text"> {PBASIC 1.0} {PBASIC 2.0}</span>
|
|
</p>
|
|
</div>
|
|
<p style="text-align: right;"><a href="../ExampleTopics/IfThenEx.htm" target="" title="" alt="" class="MCXref_0">IF...THEN Examples</a>
|
|
</p>
|
|
<p> </p>
|
|
</div>
|
|
<p class="clear"> </p>
|
|
<p> </p>
|
|
<p class="PlainText">Syntax:
|
|
<b>IF</b> <![CDATA[ ]]><i>Condition</i> <![CDATA[ ]]><b>THEN</b> <![CDATA[ ]]><i>Address</i></p>
|
|
<h2>Function</h2>
|
|
<p class="PlainText">Evaluate <i>Condition</i> and, if it is true, go to the point in the program marked
|
|
by <i>Address</i>.
|
|
|
|
</p>
|
|
<ul>
|
|
<li value="1"><b><i>Condition</i></b> is a statement, such as "x = 7" that can be evaluated
|
|
as True or False. The <i>Condition</i> can be a very simple or very complex
|
|
relationship, as described below.</li>
|
|
<li value="2"><b><i>Address</i></b> is a label that specifies where to go in the event that
|
|
<i>Condition</i> is true.</li>
|
|
</ul>
|
|
<p class="PlainText">Note: See <a href="IF_THEN_ELSE.htm" target="" title="" alt="" class="MCXref_0">IF…THEN…ELSE</a> for all BS2 models and PBASIC 2.5.</p>
|
|
<h2>Quick Facts</h2>
|
|
<table width="100%" cellpadding="4" cellspacing="0" border="1">
|
|
<tr>
|
|
<td width="20%" align="center" bgcolor="#CFCFCF"> </td>
|
|
<td width="30%" align="center" bgcolor="#CFCFCF">BS1</td>
|
|
<td width="30%" align="center" bgcolor="#CFCFCF">BS2 Family</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" bgcolor="#CFCFCF">Comparison operators</td>
|
|
<td align="center">=, <>, >, <, >=, <=</td>
|
|
<td align="center">=, <>, >, <, >=, <=</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" bgcolor="#CFCFCF">Conditional Logic Operators</td>
|
|
<td align="center">AND, OR</td>
|
|
<td align="center">NOT, AND, OR, XOR</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" bgcolor="#CFCFCF">Format of <i>Condition</i></td>
|
|
<td align="center"><i>Variable Comparison Value</i>;<br /> where <i>Value</i> is a variable or constant</td>
|
|
<td align="center"><i>Value1 Comparison Value2</i>;<br /> where <i>Value1</i> and <i>Value2</i> can be any of variable, constant or expression</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" bgcolor="#CFCFCF">Parentheses</td>
|
|
<td align="center">Not Allowed</td>
|
|
<td align="center">Allowed</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
|
|
<td align="center" colspan="1">
|
|
<p colspan="1" align="center">None</p>
|
|
</td>
|
|
<td align="center" colspan="1">
|
|
<p colspan="1" align="center"><a href="IF_THEN_ELSE.htm" target="" title="" alt="" class="MCXref_0">IF…THEN…ELSE</a>, <a href="SELECT_CASE.htm" target="" title="" alt="" class="MCXref_0">SELECT...CASE</a></p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<h2>Explanation</h2>
|
|
<p class="PlainText"><span class="keyword_in_text">IF...THEN</span> is PBASIC's primary decision maker. It tests a condition and, if
|
|
that condition is true, goes to a point in the program specified by an address label.
|
|
The condition that <span class="keyword_in_text">IF...THEN</span> tests is written as a mixture of comparison
|
|
and logic operators. The available comparison operators are: </p>
|
|
<center>
|
|
<table cellpadding="4" cellspacing="0" border="1">
|
|
<tr>
|
|
<td width="130" align="center" bgcolor="#CFCFCF">Comparison Operator Symbol</td>
|
|
<td width="170" align="center" bgcolor="#CFCFCF">Definition</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">=</td>
|
|
<td align="center">Equal</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center"><></td>
|
|
<td align="center">Not Equal</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">></td>
|
|
<td align="center">Greater Than</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center"><</td>
|
|
<td align="center">Less Than</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">>=</td>
|
|
<td align="center">Greater Than or Equal To</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center"><=</td>
|
|
<td align="center">Less Than or Equal To</td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
<p> </p>
|
|
<p class="PlainText">Comparisons are always written in the form: <i>Value1 Comparison Value2</i>. The
|
|
values to be compared can be any combination of variables (any size), constants,
|
|
or expressions. </p>
|
|
<p class="PlainText">
|
|
<img src="../../graphics/bs1note.gif" style="vertical-align: super;" /> Note: On the BS1, expressions are not allowed as arguments, and
|
|
<i>Value1</i> (to the left of comparison) must be a variable.</p>
|
|
<p class="PlainText">The following example is an <span class="keyword_in_text">IF...THEN</span> command with a simple condition:</p><pre class="BScode" xml:space="preserve">
|
|
IF 10 < 200 THEN Main
|
|
</pre>
|
|
<p class="PlainText">This code will compare the number 10 to the number 200. If 10 is less than 200,
|
|
the condition is true. In this case, 10 is less than 200 (and always will be),
|
|
so the program will jump (or <span class="keyword_in_text">GOTO</span>) the label called Main. Of course, this
|
|
is a silly example (10 is always less than 200 so this line will always cause a
|
|
jump to Main). Most of the time, you'll use at least one variable in your condition:</p>
|
|
<p>
|
|
<img src="../../graphics/bs1_inline.gif" border="0">
|
|
</img>
|
|
</p><pre class="BScode" xml:space="preserve">
|
|
SYMBOL value = W1
|
|
|
|
Main:
|
|
PULSIN 0, 1, value
|
|
DEBUG #value, CR
|
|
IF value < 4000 THEN Main
|
|
DEBUG "Value was greater than 4000!"
|
|
END
|
|
</pre>
|
|
<p>
|
|
<img src="../../graphics/bs2all_inline.gif" border="0">
|
|
</img>
|
|
</p><pre class="BScode" xml:space="preserve">
|
|
value VAR Word
|
|
|
|
Main:
|
|
PULSIN 0, 1, value
|
|
DEBUG DEC value, CR
|
|
IF (value < 4000) THEN Main
|
|
DEBUG "Value was greater than 4000!"
|
|
END
|
|
</pre>
|
|
<p class="PlainText">Here, the BASIC Stamp will look for and measure a pulse on I/O pin 0, then compare
|
|
the result, value, against 4000. If value is less than (<) 4000, it will jump
|
|
back to Main. Each time through the loop, it displays the measured value and once
|
|
it is greater than or equal to 4000, it displays, "Value was greater than 4000!"</p>
|
|
<p>
|
|
<img src="../../graphics/bs2all_inline.gif" border="0">
|
|
</img>
|
|
</p>
|
|
<p class="PlainText">On the BS2, BS2e, BS2sx, BS2p, and BS2pe, the values can be expressions as well.
|
|
This leads to very flexible and sophisticated comparisons. The <span class="keyword_in_text">IF...THEN</span>
|
|
statement below is functionally the same as the one in the program above:</p><pre class="BScode" xml:space="preserve">
|
|
IF (value < (45 * 100 - (25 * 20))) THEN Main
|
|
</pre>
|
|
<p>
|
|
<img src="../../graphics/bs2all_inline.gif" border="0">
|
|
</img>
|
|
</p>
|
|
<p class="PlainText">Here the BASIC Stamp evaluates the expression: 45 * 100 = 4500, 25 * 20 = 500,
|
|
and 4500 - 500 = 4000. Then the BASIC Stamp performs the comparison: is value <
|
|
4000? Another example that is functionally the same:</p><pre class="BScode" xml:space="preserve">
|
|
IF ((value / 100) < 40) THEN Main
|
|
</pre>
|
|
<p>
|
|
<img src="../../graphics/bs2all_inline.gif" border="0">
|
|
</img>
|
|
</p>
|
|
<p class="PlainText">It's important to realize that all comparisons are performed using unsigned,
|
|
16-bit math. This can lead to strange results if you mix signed and unsigned numbers
|
|
in <span class="keyword_in_text">IF...THEN</span> conditions. Watch what happens here when we include a signed
|
|
number (-99):</p><pre class="BScode" xml:space="preserve">
|
|
IF (-99 < 100) THEN Is_Less
|
|
DEBUG "Greater than or equal to 100"
|
|
END
|
|
|
|
Is_Less:
|
|
DEBUG "Less than 100"
|
|
END
|
|
|
|
|
|
</pre>
|
|
<p class="PlainText">Although -99 is obviously less than 100, the program will say it is greater.
|
|
The problem is that -99 is internally represented as the two's complement value
|
|
65437, which (using unsigned math) is greater than 100. This phenomena will occur
|
|
whether or not the negative value is a constant, variable or expression. </p>
|
|
<p class="PlainText"><span class="keyword_in_text">IF...THEN</span> supports the conditional logic operators NOT, AND, OR, and XOR.
|
|
See the table below for a list of the operators and their effects. NOTE: The NOT
|
|
and XOR operators are not available on the BS1.</p>
|
|
<p class="PlainText">The NOT operator inverts the outcome of a condition, changing False to True,
|
|
and True to False. The following <span class="keyword_in_text">IF...THEN</span>s are equivalent: </p><pre class="BScode" xml:space="preserve">
|
|
IF (x <> 100) THEN Not_Equal ' Jump to Not_Equal if x is not 100
|
|
IF NOT (x = 100) THEN Not_Equal ' Jump to Not_Equal if x is not 100
|
|
</pre>
|
|
<p>
|
|
<img src="../../graphics/bs1_inline.gif" border="0">
|
|
</img>
|
|
</p><pre class="BScode" xml:space="preserve">
|
|
SYMBOL value1 = B2
|
|
SYMBOL value2 = B3
|
|
|
|
Setup:
|
|
value1 = 5
|
|
value2 = 9
|
|
|
|
Main:
|
|
IF value1 = 5 AND value2 = 10 THEN Is_True ' change AND to OR and see
|
|
DEBUG "Statement was false." ' what happens
|
|
END
|
|
|
|
Is_True:
|
|
DEBUG "Statement was true."
|
|
END
|
|
</pre>
|
|
<p>
|
|
<img src="../../graphics/bs2all_inline.gif" border="0">
|
|
</img>
|
|
</p><pre class="BScode" xml:space="preserve">
|
|
value1 VAR Byte
|
|
value2 VAR Byte
|
|
|
|
Setup:
|
|
value1 = 5
|
|
value2 = 9
|
|
|
|
Main:
|
|
' Change AND to OR and see what happens
|
|
IF (value1 = 5) AND (value2 = 10) THEN Is_True
|
|
DEBUG "Statement was false."
|
|
END
|
|
|
|
Is_True:
|
|
DEBUG "Statement was true."
|
|
END
|
|
</pre>
|
|
<p class="PlainText">The condition "(value1 = 5) AND (value2 = 10)" is not true. Although value1 is
|
|
5, value2 is not 10. The AND operator works just as it does in English; both
|
|
conditions must be true for the statement to be true. The OR operator also works
|
|
in a familiar way; if one or the other or both conditions are true, then the
|
|
statement is true. The XOR operator (short for exclusive-OR) may not be familiar,
|
|
but it does have an English counterpart: If one condition or the other (but not
|
|
both) is true, then the statement is true. </p>
|
|
<p class="PlainText">The table below summarizes the effects of the conditional logic operators. As
|
|
with math, you can alter the order in which comparisons and logical operations
|
|
are performed by using parentheses. Operations are normally evaluated left-to-right.
|
|
Putting parentheses around an operation forces PBASIC 2.0 to evaluate it before
|
|
operations not in parentheses. </p>
|
|
<p class="PlainText">
|
|
<img src="../../graphics/bs1note.gif" style="vertical-align: super;" /> Note: On the BS1, parentheses are not allowed within
|
|
arguments.</p>
|
|
<center>
|
|
<table cellpadding="4" cellspacing="0" border="1">
|
|
<tr>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">Condition A</td>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">NOT A</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">False</td>
|
|
<td align="center">True</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">True</td>
|
|
<td align="center">False</td>
|
|
</tr>
|
|
</table>
|
|
<br>
|
|
</br>
|
|
<table cellpadding="4" cellspacing="0" border="1">
|
|
<tr>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">Condition A</td>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">Condition B</td>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">A AND B</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">False</td>
|
|
<td align="center">False</td>
|
|
<td align="center">False</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">False</td>
|
|
<td align="center">True</td>
|
|
<td align="center">False</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">True</td>
|
|
<td align="center">False</td>
|
|
<td align="center">False</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">True</td>
|
|
<td align="center">True</td>
|
|
<td align="center">True</td>
|
|
</tr>
|
|
</table>
|
|
<br>
|
|
</br>
|
|
<table cellpadding="4" cellspacing="0" border="1">
|
|
<tr>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">Condition A</td>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">Condition B</td>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">A OR B</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">False</td>
|
|
<td align="center">False</td>
|
|
<td align="center">False</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">False</td>
|
|
<td align="center">True</td>
|
|
<td align="center">True</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">True</td>
|
|
<td align="center">False</td>
|
|
<td align="center">True</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">True</td>
|
|
<td align="center">True</td>
|
|
<td align="center">True</td>
|
|
</tr>
|
|
</table>
|
|
<br>
|
|
</br>
|
|
<table cellpadding="4" cellspacing="0" border="1">
|
|
<tr>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">Condition A</td>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">Condition B</td>
|
|
<td width="100" align="center" bgcolor="#CFCFCF">A XOR B</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">False</td>
|
|
<td align="center">False</td>
|
|
<td align="center">False</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">False</td>
|
|
<td align="center">True</td>
|
|
<td align="center">True</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">True</td>
|
|
<td align="center">False</td>
|
|
<td align="center">True</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center">True</td>
|
|
<td align="center">True</td>
|
|
<td align="center">False</td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
<p> </p>
|
|
<p class="PlainText">Unlike the <span class="keyword_in_text">IF...THEN</span> commands in other BASIC language variants, PBASIC 1.0/2.0's
|
|
<span class="keyword_in_text">IF...THEN</span> can only go to a label as the result of a decision. It cannot
|
|
conditionally perform some instruction, as in "IF x < 20 THEN y = y + 1" (This
|
|
is possible using the $PBASIC 2.5 directive). To achieve this in PBASIC 1.0 and 2.0,
|
|
you have to invert the logic using NOT and skip over the conditional instruction
|
|
unless the condition is met: </p><pre class="BScode" xml:space="preserve">
|
|
IF NOT (x < 20) THEN No_Inc ' Don't increment y unless x < 20.
|
|
y = y + 1 ' Increment y if x < 20.
|
|
|
|
No_Inc: ' Program continues.
|
|
</pre>
|
|
<p class="PlainText">You can also code a conditional <span class="keyword_in_text">GOSUB</span>, as in "<span class="code_in_text">IF (x = 100) THEN GOSUB Centennial</span>."
|
|
In PBASIC 2.0:</p><pre class="BScode" xml:space="preserve">
|
|
IF NOT (x = 100) THEN No_Cent
|
|
GOSUB Centennial ' IF x = 100 THEN GOSUB Centennial.
|
|
|
|
No_Cent: ' Program continues.
|
|
</pre>
|
|
<p class="PlainText">Internally, the BASIC Stamp defines "False" as 0 and "True" as any value other
|
|
than 0. Consider the following instructions: </p><pre class="BScode" xml:space="preserve">
|
|
flag VAR Bit
|
|
|
|
Setup:
|
|
flag = 1
|
|
|
|
Test:
|
|
IF flag THEN Is_True
|
|
|
|
Is_False:
|
|
DEBUG "False", CR
|
|
END
|
|
|
|
Is_True:
|
|
DEBUG "True"
|
|
END
|
|
</pre>
|
|
<p class="PlainText">Since flag is 1, <span class="keyword_in_text">IF...THEN</span> would evaluate it as true and print the message
|
|
"True" on the screen. Suppose you changed the <span class="keyword_in_text">IF...THEN</span> command to read
|
|
"<span class="code_in_text">IF NOT (flag) THEN IsTrue</span>." That would also evaluate as True. Whoa! Isn't NOT 1
|
|
the same thing as 0? No, at least not in the 16-bit world of the BASIC Stamp.</p>
|
|
<p class="PlainText">Internally, the BASIC Stamp sees a bit variable containing 1 as the 16-bit number
|
|
%0000000000000001. So it sees the NOT of that as %1111111111111110. Since any
|
|
non-zero number is regarded as True, NOT 1 is True. Strange but true. </p>
|
|
<p class="PlainText">The easiest way to avoid the kinds of problems this might cause is to always
|
|
use a conditional operator with <span class="keyword_in_text">IF...THEN</span>. Change the example above to read
|
|
<span class="code_in_text">IF (flag = 1) THEN Is_True</span>. The result of the comparison will follow <span class="keyword_in_text">IF...THEN</span>rules. Also, the logical operators will work as they should; <span class="code_in_text">IF NOT (flag = 1)
|
|
THEN Is_True</span> will correctly evaluate to False when flag contains 1.</p>
|
|
<p class="PlainText">This also means that you should only use the "named" conditional logic operators
|
|
NOT, AND, OR, and XOR with <span class="keyword_in_text">IF...THEN</span>. The conditional logic operators format
|
|
their results correctly for <span class="keyword_in_text">IF...THEN</span> instructions. The other logical
|
|
operators, represented by symbols ~ & | and ^ do not; they are binary logic operators.</p>
|
|
<div class="Col2">
|
|
<div class="MasterFoot">
|
|
<p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div class="Col2">
|
|
<div class="MasterFoot">
|
|
<p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p>
|
|
<p style="text-align: right;">Copyright © <span class="ContactInfoCompanyName">Parallax Inc.</span></p>
|
|
<p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">/* <![CDATA[ */
|
|
|
|
var _gaq = _gaq || [];
|
|
_gaq.push(['_setAccount', 'UA-285614-1']);
|
|
_gaq.push(['_trackPageview']);
|
|
|
|
(function() {
|
|
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
|
})();
|
|
|
|
/* ]]> */</script>
|
|
<script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
|
|
</script>
|
|
</body>
|
|
</html> |