148 lines
8.5 KiB
HTML
148 lines
8.5 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>DO…LOOP</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">DO…LOOP</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 2.5}</span>
|
|||
|
</p>
|
|||
|
</div>
|
|||
|
<p style="text-align: right;"><a href="../ExampleTopics/DoLoopEx.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">DO...LOOP Example</a>
|
|||
|
</p>
|
|||
|
<p> </p>
|
|||
|
</div>
|
|||
|
<p class="clear"> </p>
|
|||
|
<p> </p>
|
|||
|
<p class="PlainText">Syntax:</p>
|
|||
|
<p class="PlainText"><span class="keyword_in_text">DO </span>{ <span class="keyword_in_text">WHILE</span> | <span class="keyword_in_text">UNTIL</span> <![CDATA[ ]]><i>condition(s)</i> }<br />     <i>Statement(s)</i><br />   <span class="keyword_in_text">LOOP</span> { <span class="keyword_in_text">WHILE</span> | <span class="keyword_in_text">UNTIL</span> <![CDATA[ ]]><i>condition(s)</i> }
|
|||
|
</p>
|
|||
|
<h2>Function</h2>
|
|||
|
<p class="PlainText">Create a repeating loop that executes the program lines between <span class="keyword_in_text">DO</span> and
|
|||
|
<span class="keyword_in_text">LOOP</span>, optionally testing before or after the loop statements
|
|||
|
|
|||
|
</p>
|
|||
|
<ul>
|
|||
|
<li value="1"><b><i>Condition</i></b> is an optional variable/constant/expression (0 -
|
|||
|
65535) which determines whether the loop will run or terminate.</li>
|
|||
|
<li value="2"><b><i>Statement</i></b> is any valid PBASIC statement.</li>
|
|||
|
</ul>
|
|||
|
<h2>Quick Facts<br /></h2>
|
|||
|
<table width="100%" cellpadding="4" cellspacing="0" border="1">
|
|||
|
<tr>
|
|||
|
<td width="26%" align="center" bgcolor="#CFCFCF"> </td>
|
|||
|
<td width="64%" align="center" bgcolor="#CFCFCF">BS2 Family</td>
|
|||
|
</tr>
|
|||
|
<tr>
|
|||
|
<td align="center" bgcolor="#CFCFCF">Maximum nested loops</td>
|
|||
|
<td align="center">16</td>
|
|||
|
</tr>
|
|||
|
<tr>
|
|||
|
<td align="center" bgcolor="#CFCFCF">WHILE <i>Condition</i> evaluation</td>
|
|||
|
<td align="center">Run loop if <i>Condition</i> evaluates as True (1)</td>
|
|||
|
</tr>
|
|||
|
<tr>
|
|||
|
<td align="center" bgcolor="#CFCFCF">UNTIL <i>Condition</i> evaluation</td>
|
|||
|
<td align="center">Terminate loop if <i>Condition</i> evaluates as True (1)</td>
|
|||
|
</tr>
|
|||
|
<tr>
|
|||
|
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
|
|||
|
<td align="center" colspan="1">
|
|||
|
<p colspan="1" align="center"><a href="EXIT.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">EXIT</a>, <a href="FOR_NEXT.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">FOR…NEXT</a></p>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</table>
|
|||
|
<h2>Explanation</h2>
|
|||
|
<p class="PlainText"><span class="keyword_in_text">DO...LOOP</span> loops let your program execute a series of instructions
|
|||
|
indefinitely, or until a specified condition terminates the loop. The simplest
|
|||
|
form is shown here: </p><pre class="BScode" xml:space="preserve">
|
|||
|
Error_Message:
|
|||
|
DO
|
|||
|
DEBUG "Error...", CR
|
|||
|
PAUSE 2000
|
|||
|
LOOP
|
|||
|
</pre>
|
|||
|
<p class="PlainText">In this example the error message will be printed on the
|
|||
|
<a href="DEBUG.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">DEBUG</a> screen every two seconds until the BASIC Stamp is
|
|||
|
reset. Simple <span class="keyword_in_text">DO...LOOP</span> loops can be terminated with
|
|||
|
<a href="EXIT.htm" class="MCXref_0" xrefformat="{paratext}">EXIT</a>. For example:</p><pre class="BScode" xml:space="preserve">
|
|||
|
Error_Message:
|
|||
|
DO
|
|||
|
DEBUG "Error...", CR
|
|||
|
PAUSE 1000
|
|||
|
IF (AckPin = 0) THEN EXIT ' wait for user button press
|
|||
|
LOOP
|
|||
|
GOTO Initialize ' re-initialize system
|
|||
|
</pre>
|
|||
|
<p class="PlainText">In this case the loop will continue until the pin named AckPin is pulled low,
|
|||
|
then the loop will terminate and continue at the line <span class="code_in_text">GOTO Initialize</span>.</p>
|
|||
|
<p class="PlainText">More often than not, you will want to test some condition to determine whether
|
|||
|
the loop code should run or continue to run. A loop that tests the condition before
|
|||
|
running loop code is constructed like this:</p><pre class="BScode" xml:space="preserve">
|
|||
|
reps VAR Nib
|
|||
|
|
|||
|
Print_Stars:
|
|||
|
DO WHILE (reps < 3) ' test before loop statements
|
|||
|
DEBUG "*"
|
|||
|
reps = reps + 1
|
|||
|
LOOP
|
|||
|
</pre>
|
|||
|
<p class="PlainText">In this program the loop code <span class="code_in_text">DEBUG "*"</span> will not run unless the <span class="keyword_in_text">WHILE</span>
|
|||
|
condition evaluates as True. Another way to write the loop is like this:</p><pre class="BScode" xml:space="preserve">
|
|||
|
reps VAR Nib
|
|||
|
|
|||
|
Print_Stars:
|
|||
|
DO
|
|||
|
DEBUG "*"
|
|||
|
reps = reps + 1
|
|||
|
LOOP UNTIL (reps >= 3) ' test after loop statements
|
|||
|
</pre>
|
|||
|
<p class="PlainText">The difference is that with this loop, the loop statements will always run at
|
|||
|
least once before the condition is tested and will continue to as long as the
|
|||
|
<span class="keyword_in_text">UNTIL</span> condition evaluates as False.</p>
|
|||
|
<p class="PlainText">Note that <span class="keyword_in_text">WHILE</span> (loop runs while True) and <span class="keyword_in_text">UNTIL</span> (loop runs until
|
|||
|
True) tests can be interchanged, but are generally used as illustrated above.
|
|||
|
</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>
|