00001 #include "MusimatTutorial.h" 00002 MusimatTutorialSection(B0118) { 00003 Print("*** B.1.18 Iteration ***"); 00004 /***************************************************************************** 00005 00006 B.1.18 Iteration 00007 00008 We must be able to repeat a statement or statements multiple times. For example, Euclid's method 00009 returns to step 1 from step 3, depending upon the value of variable r (see section 9.2.2). In 00010 Musimat, the Repeat statement causes a statement or compound statement to repeat intermina- 00011 bly. This allows us to implement Euclid's method as follows: 00012 *****************************************************************************/ 00013 00014 Integer m = 103; 00015 Integer n = 17; 00016 Integer r; 00017 00018 Repeat{ 00019 r = Mod(m, n); //remainder of m divided by n 00020 If (r == 0) { 00021 // Halt(n); // halt, and give answer n 00022 Break; // break, and give answer n 00023 } Else { 00024 m = n; 00025 n = r; 00026 } 00027 } 00028 Print(n); 00029 00030 /***************************************************************************** 00031 This code shows an example of nested compound statement lists. The bare syntax of this example is 00032 00033 Repeat {... If (...) {...} Else {...} 00034 00035 and the compound statements following If and Else are nested inside the compound statement 00036 following Repeat. We can nest compound statements as deeply as we desire. 00037 00038 Since it never stops by itself, the only way to terminate a Repeat statement is with a Halt state- 00039 ment (or Return statement, see below). It's a crude but effective technique; 00040 however, there are more elegant ways to decide how 00041 many times to repeat a block of statements. The Do-While statement allows us to specify a ter- 00042 mination condition that is evaluated after the body has been executed. Here is an example that 00043 prints the random value assigned to x and repeats for as long as x is less than 0.9. 00044 *****************************************************************************/ 00045 00046 Real x; 00047 Do { 00048 x = Random(); // choose a random value between 0.0 and 1.0 00049 Print(x); 00050 } While (x < 0.9); 00051 00052 /***************************************************************************** 00053 Because Random() returns a uniform random value in the range 0.0 to 1.0, its value will be less 00054 than 0.9 on average 90 percent of the time. It is possible, though unlikely, that this statement would 00055 print its value only once, and it is also possible that it could print dozens, even hundreds, of times before 00056 halting, depending upon the particular sequence of random numbers returned by Random(). 00057 00058 The For statement also implements a way of repeating a statement or compound statement a 00059 number of times, but it allows us to directly manage the value of one or more variables each time 00060 the statements are executed and to use them to determine when to stop. This example prints the 00061 integers between 0 and 9: 00062 *****************************************************************************/ 00063 00064 Integer i; 00065 For (i = 0; i < 10; i = i + 1) { 00066 Print(i); 00067 } 00068 00069 /***************************************************************************** 00070 The variable i is called the control variable. The example first sets i to 0, then tests if i < 10. 00071 Since 0 < 10, the Print() statement is executed. Next, the For statement executes the state- 00072 ment i = i + 1, which adds 1 to the value of i. So now i equals 1. Again, the For loop tests 00073 if i < 10, and since 1 < 10, it executes Print() again. It again adds 1 to the value of i. So now 00074 i equals 2. This process continues until i == 10, whereupon the For loop terminates because 00075 then i < 10 is False. 00076 00077 The For statement is a little twisty, so let's take a more careful look at its operation. In general, 00078 we can name the parts of the For statement as follows: 00079 00080 For (initialization; test; change) 00081 statement 00082 00083 where statement can be a single statement (terminated by a semicolon) or a compound statement 00084 (enclosed with curly braces). 00085 00086 The For statement first executes the initialization code, then 00087 evaluates the boolean expression test. If the value of test is False, the For statement terminates. 00088 If the value of test is True, the statement is executed, then the change expression is executed, 00089 and finally the test is evaluated again. If the value of test is False, the For statement terminates. 00090 If the value of test is True, the cycle repeats again and again until the value of test is False. 00091 00092 As a convenience, it is possible to define and set the value of the initialization variable 00093 in one step, so the preceding example could have been written 00094 *****************************************************************************/ 00095 00096 For (Integer i = 0; i < 10; i = i + 1) { 00097 Print(i); 00098 } 00099 00100 /***************************************************************************** 00101 Warning: the simple form of For statement is dangerous if you are not careful. 00102 For example, here is a common mistake. By the way it is indented, it appears the 00103 programmer perhaps wants that the Print(i * 10) statement to be executed 00104 as part of the For statement. But it's only executed 00105 once after the For statement prints the value of i ten times. 00106 *****************************************************************************/ 00107 00108 For (Integer i = 0; i < 10; i = i + 1) 00109 Print(i); 00110 Print(i * 10); 00111 00112 /***************************************************************************** 00113 As a consequence, a good habit is to always use the compound For statement, showing 00114 with brackets explicitly what statements you intend to include in the For statement: 00115 *****************************************************************************/ 00116 00117 For (Integer i = 0; i < 10; i = i + 1) { 00118 Print(i); 00119 Print(i * 10); 00120 } 00121 00122 00123 } 00124 00126 /* $Revision: 1.2 $ $Date: 2006/09/05 06:32:25 $ $Author: dgl $ $Name: $ $Id: B0118.cpp,v 1.2 2006/09/05 06:32:25 dgl Exp $ */ 00127 // The Musimat Tutorial © 2006 Gareth Loy 00128 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00129 // and published exclusively by The MIT Press. 00130 // This program is released WITHOUT ANY WARRANTY; without even the implied 00131 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00132 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00133 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00134 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00135 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00136 // The Musimat website: http://www.musimat.com/ 00137 // This program is released under the terms of the GNU General Public License 00138 // available here: http://www.gnu.org/licenses/gpl.txt 00139