Files
RCBASIC4/doc/doc_files/loops.html
2024-10-24 23:25:02 -04:00

151 lines
5.6 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td { border: 1px solid black; } th, td { padding: 10px; }
body { background-color: #064066; color: #FFFFFF; }
a { color: #40bfb8; }
a::before {
content: "---";
font-family: monospace, monospace;
display: inline-block;
margin-right: 6px;
color: #064066;
}
.rc_number { color: #0b9898; }
.rc_string { color: #dd4040; }
.rc_keyword { color: #6084a8; font-weight: bold; }
.rc_comment { color: #6e716e; }
#rc_code { font-family: Consolas,"courier new"; background-color: #2d3335; padding: 2px; font-size: 105%; }
ul, #myUL {
list-style-type: none;
}
#myUL {
margin: 0;
padding: 0;
}
.box {
cursor: pointer;
-webkit-user-select: none; /* Safari 3.1+ */
-moz-user-select: none; /* Firefox 2+ */
-ms-user-select: none; /* IE 10+ */
user-select: none;
}
.box::before {
content: "[+]";
font-family: monospace, monospace;
font-weight: bold;
color: #40bfb8;
display: inline-block;
margin-right: 6px;
}
.box {
font-weight: bold;
color: #40bfb8;
text-decoration: underline;
}
.check-box::before {
content: "[-]";
font-family: monospace, monospace;
font-weight: bold;
//color: dodgerblue;
color: #40bfb8;
}
.nested {
display: none;
}
.active {
display: block;
}
</style>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>RCBasic Loops [RCBasic Doc] </title>
</head>
<body>
<p><h1>LOOPS </h1></p>
<p>
RC BASIC has 3 types of loops: <b>FOR</b>, <b>WHILE</b>, and <b>DO</b>.
</p>
<p>
The <b>FOR</b> loop repeats a block of code and increments a counter each time it finishes the block between <b>FOR</b> and <b>NEXT</b>. When the counter reaches a certain value the <b>FOR</b> loop stops.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">For</span>&nbsp;I&nbsp;=&nbsp;<span class="rc_number">1</span>&nbsp;<span class="rc_keyword">To</span>&nbsp;<span class="rc_number">5</span>&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Print</span>&nbsp;I&nbsp;<br>
<span class="rc_keyword">Next</span>&nbsp;<br>
</code></p>
<p>
The above code will output the numbers 1 to 5 to the console. Optionally you could use the <b>STEP</b> keyword to set the number that the counter will increase by. Look at the following:
</p>
<p id="rc_code"><code>
<span class="rc_keyword">For</span>&nbsp;I&nbsp;=&nbsp;<span class="rc_number">1</span>&nbsp;<span class="rc_keyword">To</span>&nbsp;<span class="rc_number">5</span>&nbsp;Step&nbsp;<span class="rc_number">2</span>&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Print</span>&nbsp;I&nbsp;<br>
<span class="rc_keyword">Next</span>&nbsp;<br>
</code></p>
<p>
The above code will output the numbers 1, 3, and 5 to the console. The <b>STEP</b> keyword increases I by 2 each time through the <b>FOR</b> loop.
</p>
<p>
<b>WHILE</b> loops will execute a block of code while a certain condition is true.
</p>
<p id="rc_code"><code>
I&nbsp;=&nbsp;<span class="rc_number">0</span>&nbsp;<br>
<span class="rc_keyword">While</span>&nbsp;I&nbsp;&lt;&nbsp;<span class="rc_number">5</span>&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Print</span>&nbsp;I&nbsp;<br>
&nbsp;&nbsp;&nbsp;I&nbsp;=&nbsp;I&nbsp;+&nbsp;<span class="rc_number">1</span>&nbsp;<br>
<span class="rc_keyword">Wend</span>&nbsp;<br>
</code></p>
<p>
The above code will output the numbers 0 to 4. It will not output the number 5 because if ( I < 5 ) is false the loop will not repeat.
</p>
<p>
The <b>DO</b> loop is similar to the <b>WHILE</b> loop with an exception. The <b>WHILE</b> loop checks for the loop condition at the start of the loop but the <b>DO</b> loop checks for the loop condition at the end of the loop. What this means is that a <b>WHILE</b> loop will never execute if the condition is false at the start but the <b>DO</b> loop is guaranteed to execute at least once before it checks if the condition was true or not.
</p>
<p>
The <b>DO</b> loop is also unique in that it has 3 different forms. Here is the simplest form of the <b>DO</b> loop.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Do</span>&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"HELLO WORLD"</span>&nbsp;<br>
<span class="rc_keyword">Loop</span>&nbsp;<br>
</code></p>
<p>
The code above will continue to output HELLO WORLD to the console infinitely.
</p>
<p id="rc_code"><code>
I&nbsp;=&nbsp;<span class="rc_number">0</span>&nbsp;<br>
<span class="rc_keyword">Do</span>&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Print</span>&nbsp;I&nbsp;<br>
&nbsp;&nbsp;&nbsp;I&nbsp;=&nbsp;I&nbsp;+&nbsp;<span class="rc_number">1</span>&nbsp;<br>
<span class="rc_keyword">Loop</span>&nbsp;<span class="rc_keyword">While</span>&nbsp;I&nbsp;&lt;&nbsp;<span class="rc_number">5</span>&nbsp;<br>
</code></p>
<p>
The above code will output the numbers 0 to 4 to the console. This form of the <b>DO</b> loop will continue to loop while the loop condition is true.
</p>
<p id="rc_code"><code>
I&nbsp;=&nbsp;<span class="rc_number">0</span>&nbsp;<br>
<span class="rc_keyword">Do</span>&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Print</span>&nbsp;I&nbsp;<br>
&nbsp;&nbsp;&nbsp;I&nbsp;=&nbsp;I&nbsp;+&nbsp;<span class="rc_number">1</span>&nbsp;<br>
<span class="rc_keyword">Loop</span>&nbsp;<span class="rc_keyword">Until</span>&nbsp;I&nbsp;=&nbsp;<span class="rc_number">5</span>&nbsp;<br>
</code></p>
<p>
The above code will output the numbers 0 to 4 to the console. This form of the <b>DO</b> loop will continue to loop until the loop condition is true.
</p>
<p>
</body>
</html>