| 1 |
<html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 |
<title>Scripting 5.4. Loops</title> |
| 3 |
<style> |
| 4 |
td { padding-left: 5pt; padding-right: 5pt; } |
| 5 |
th { padding-left: 5pt; padding-right: 5pt; } |
| 6 |
code { white-space: pre-wrap; } |
| 7 |
dd { white-space: pre-wrap; } |
| 8 |
</style> |
| 9 |
</head><body bgcolor="#FFFFFF"> |
| 10 |
|
| 11 |
<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 |
Scripting 5.4. Loops |
| 13 |
</b></font></table></table> |
| 14 |
<h2>"For" loops</h2> |
| 15 |
<dl> |
| 16 |
<dt><b>for</b> <i>variable</i> <b>from</b> <i>expression</i><sub>1</sub> <b>to</b> <i>expression</i><sub>2</sub> |
| 17 |
<dt><b>for</b> <i>variable</i> <b>to</b> <i>expression</i> |
| 18 |
<dd>the statements between the <b>for</b> line and the matching <b>endfor</b> will be executed while a variable takes on values between two expressions, with an increment (raise) of 1 on each turn of the loop. If there is no <b>from</b>, the loop variable starts at 1.</dd> |
| 19 |
</dl> |
| 20 |
<p>The following script plays nine sine waves, with frequencies of 200, 300, ..., 1000 Hz:</p> |
| 21 |
<code> <b>for</b> i <b>from</b> 2 <b>to</b> 10<br></code> |
| 22 |
<code> Create Sound as pure tone: "tone", 1, 0, 0.3, 44100, i * 100, 0.2, 0.01, 0.01<br></code> |
| 23 |
<code> Play<br></code> |
| 24 |
<code> Remove<br></code> |
| 25 |
<code> <b>endfor</b><br></code> |
| 26 |
<p>The stop value of the <b>for</b> loop is evaluated on each turn. If the second expression is already less than the first expression to begin with, the statements between <b>for</b> and <b>endfor</b> are not executed even once.</p> |
| 27 |
<h2>“Repeat” loops</h2> |
| 28 |
<dl> |
| 29 |
<dt><b>until</b> <i>expression</i> |
| 30 |
<dd>the statements between the matching preceding <b>repeat</b> and the <b>until</b> line will be executed again if the expression evaluates to zero or <i>false</i>.</dd> |
| 31 |
</dl> |
| 32 |
<p>The following script measures the number of trials it takes me to throw 12 with two dice:</p> |
| 33 |
<code> throws = 0<br></code> |
| 34 |
<code> <b>repeat</b><br></code> |
| 35 |
<code> eyes = randomInteger (1, 6) + randomInteger (1, 6)<br></code> |
| 36 |
<code> throws = throws + 1<br></code> |
| 37 |
<code> <b>until</b> eyes = 12<br></code> |
| 38 |
<code> writeInfoLine: "It took me ", throws, " trials to throw 12 with two dice."<br></code> |
| 39 |
<p>The statements in the <b>repeat</b>/<b>until</b> loop are executed at least once.</p> |
| 40 |
<h2>"While" loops</h2> |
| 41 |
<dl> |
| 42 |
<dt><b>while</b> <i>expression</i> |
| 43 |
<dd>if the expression evaluates to zero or <i>false</i>, the execution of the script jumps after the matching <b>endwhile</b>.</dd> |
| 44 |
<dt><b>endwhile</b> |
| 45 |
<dd>execution jumps back to the matching preceding <b>while</b> line, which is then evaluated again.</dd> |
| 46 |
</dl> |
| 47 |
<p>The following script forces the number <i>x</i> into the range [0; 2π):</p> |
| 48 |
<code> <b>while</b> x < 0<br></code> |
| 49 |
<code> x = x + 2 * pi<br></code> |
| 50 |
<code> <b>endwhile</b><br></code> |
| 51 |
<code> <b>while</b> x >= 2 * pi<br></code> |
| 52 |
<code> x = x - 2 * pi<br></code> |
| 53 |
<code> <b>endwhile</b><br></code> |
| 54 |
<p>If the expression evaluates to zero or <i>false</i> to begin with, the statements between <b>while</b> and <b>endwhile</b> are not executed even once.</p> |
| 55 |
<h3>Links to this page</h3> |
| 56 |
<ul> |
| 57 |
<li><a href="Scripting.html">Scripting</a> |
| 58 |
<li><a href="Scripting_5__Language_elements_reference.html">Scripting 5. Language elements reference</a> |
| 59 |
<li><a href="Scripting_5_5__Procedures.html">Scripting 5.5. Procedures</a> |
| 60 |
</ul> |
| 61 |
<hr> |
| 62 |
<address> |
| 63 |
<p>© ppgb 20140111</p> |
| 64 |
</address> |
| 65 |
</body> |
| 66 |
</html> |