This repository has been archived on 2024-09-22. You can view files and clone it, but cannot push or open issues or pull requests.
pbi-ide/help/BasicStampHelp/Content/LanguageTopics/Commands/IF_THEN_ELSE.htm

181 lines
10 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…ELSE</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…ELSE</h1>
<div class="ImagePlusCaption">
<div class="Col2">
<p>
<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&#160;2.5}</span>
</p>
</div>
<p style="text-align: right;"><a href="../ExampleTopics/IfThenElseEx.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">IF...THEN...ELSE Example</a>
</p>
<p>&#160;</p>
</div>
<p class="clear">&#160;</p>
<p>&#160;</p>
<p class="PlainText">Syntax: <br /><br />&#160; &#160;<b>IF</b> <![CDATA[ ]]><i>Condition</i> <![CDATA[ ]]><span class="keyword_in_text">THEN</span><br />&#160; &#160; &#160;<i>Statement(s)</i><br />&#160;{ <span class="keyword_in_text">ELSEIF</span> <![CDATA[ ]]><i>Condition(s)</i> <![CDATA[ ]]><span class="keyword_in_text">THEN</span><br />&#160; &#160; &#160;<i>Statement(s)</i> }<br />&#160;{ <span class="keyword_in_text">ELSE</span><br />&#160; &#160; &#160;<i>Statement(s)</i> }<br />&#160; &#160;<span class="keyword_in_text">ENDIF</span><br /><br />&#160; &#160;<b>IF</b> <![CDATA[ ]]><i>Condition</i> <![CDATA[ ]]><span class="keyword_in_text">THEN</span> <![CDATA[ ]]><i>Statement(s)</i>{ <span class="keyword_in_text">ELSEIF</span> <![CDATA[ ]]><i>Statement(s)</i> } { <span class="keyword_in_text">ELSE</span> <![CDATA[ ]]><i>Statement(s)</i> }</p>
<h2>Function</h2>
<p class="PlainText">Evaluate <i>Condition</i> and, if it is true, execute the statement(s) following
<span class="keyword_in_text">THEN</span>, otherwise jump to and evaluate the <span class="keyword_in_text">ELSEIF</span> condition. If no
<span class="keyword_in_text">ELSEIF</span> statement/block is provided, jump to and execute the statements that
follow <span class="keyword_in_text">ELSE</span>. If no <span class="keyword_in_text">ELSE</span> block is provided, the program will continue
at the line that follows <span class="keyword_in_text">ENDIF</span> (or the next line when single-line syntax
is used).
</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>Statement</i></b> is any valid PBASIC statement. Multiple statements
may be placed on the same line (though not recommended) by separating each
statement with a colon ( : ).</li>
</ul>
<h2>Quick Facts</h2>
<table width="100%" cellpadding="4" cellspacing="0" border="1">
<tr>
<td width="20%" align="center" bgcolor="#CFCFCF">&#160;</td>
<td width="80%" align="center" bgcolor="#CFCFCF">BS2 Family</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Comparison operators</td>
<td align="center">=, &lt;&gt;, &gt;, &lt;, &gt;=, &lt;=</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Conditional Logic Operators</td>
<td align="center">NOT, AND, OR, XOR</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Format of condition</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">Allowed</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Nesting</td>
<td align="center">Up to 16 levels</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
<td align="center" colspan="1">
<p colspan="1" align="center"><a href="IF_THEN.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">IF…THEN</a>, <a href="SELECT_CASE.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">SELECT...CASE</a></p>
</td>
</tr>
</table>
<h2>Explanation</h2>
<p class="PlainText"><span class="keyword_in_text">IF...THEN...ELSE</span> is a primary PBASIC decision maker that allows one block of
code or [optionally] another to run based on the value (True or False) of a condition.
The condition that <span class="keyword_in_text">IF...THEN...ELSE</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">&lt;&gt;</td>
<td align="center">Not Equal</td>
</tr>
<tr>
<td align="center">&gt;</td>
<td align="center">Greater Than</td>
</tr>
<tr>
<td align="center">&lt;</td>
<td align="center">Less Than</td>
</tr>
<tr>
<td align="center">&gt;=</td>
<td align="center">Greater Than or Equal To</td>
</tr>
<tr>
<td align="center">&lt;=</td>
<td align="center">Less Than or Equal To</td>
</tr>
</table>
</center>
<p>&#160;</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><pre class="BScode" xml:space="preserve">
value VAR Word
Main:
DO
PULSIN 0, 1, value ' measure pulse input
DEBUG DEC value, CR
IF (value &gt; 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
</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. Based on this comparison, a message
regarding the pulse width value will be printed.</p>
<p class="PlainText">In the following example, single-line syntax is used. Note that <span class="keyword_in_text">ENDIF</span>
is not required when using single-line formatting of <span class="keyword_in_text">IF...THEN...ELSE</span>:
</p><pre class="BScode" xml:space="preserve">
DEBUG "Your grade is: "
IF (score &gt;= 90) THEN DEBUG "A", CR : ELSE DEBUG "B (or less)", CR
</pre>
<p class="PlainText">See <a href="IF_THEN.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">IF…THEN</a> for additional details on the use of
comparison and conditional 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 ©&#160;<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>