- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatTutorial/B0118.cpp

Go to the documentation of this file.

    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 Integer m = 103;
    00014 Integer n = 17;
    00015 Integer r;
    00016
    00017 Repeat{
    00018   r = Mod(m, n);   //remainder of m divided by n
    00019   If (r == 0)  {
    00020     // Halt(n);       // halt, and give answer n
    00021           Break;                // break, and give answer n
    00022   } Else  {
    00023     m = n;
    00024     n = r;
    00025   }
    00026 }
    00027 Print(n);
    00028
    00029 /*****************************************************************************
    00030 This code shows an example of nested compound statement lists. The bare syntax of this example is
    00031
    00032 Repeat {... If (...) {...} Else {...}}
    00033
    00034 and the compound statements following If and Else are nested inside the compound statement
    00035 following Repeat. We can nest compound statements as deeply as we desire.
    00036
    00037 Since it never stops by itself, the only way to terminate a Repeat statement is with a Halt state-
    00038 ment (or Return statement, see below).  It’s a crude but effective technique;
    00039 however, there are more elegant ways to decide how
    00040 many times to repeat a block of statements. The Do-While statement allows us to specify a ter-
    00041 mination condition that is evaluated after the body has been executed. Here is an example that
    00042 prints the random value assigned to x and repeats for as long as x is less than 0.9.
    00043 *****************************************************************************/
    00044
    00045 Real x;
    00046 Do {
    00047   x = Random();  // choose a random value between 0.0 and 1.0
    00048   Print(x);
    00049 } While (x < 0.9);
    00050
    00051 /*****************************************************************************
    00052 Because Random() returns a uniform random value in the range 0.0 to 1.0, its value will be less
    00053 than 0.9 on average 90 percent of the time. It is possible, though unlikely, that this statement would
    00054 print its value only once, and it is also possible that it could print dozens, even hundreds, of times before
    00055 halting, depending upon the particular sequence of random numbers returned by Random().
    00056
    00057 The For statement also implements a way of repeating a statement or compound statement a
    00058 number of times, but it allows us to directly manage the value of one or more variables each time
    00059 the statements are executed and to use them to determine when to stop. This example prints the
    00060 integers between 0 and 9:
    00061 *****************************************************************************/
    00062 Integer i;
    00063 For (i = 0; i < 10; i = i + 1)
    00064   Print(i);
    00065
    00066 /*****************************************************************************
    00067 The variable i is called the control variable. The example first sets i to 0, then tests if i < 10.
    00068 Since 0 < 10, the Print() statement is executed. Next, the For statement executes the state-
    00069 ment i = i + 1, which adds 1 to the value of i. So now i equals 1. Again, the For loop tests
    00070 if i < 10, and since 1 < 10, it executes Print() again. It again adds 1 to the value of i. So now
    00071 i equals 2. This process continues until i == 10, whereupon the For loop terminates because
    00072 then i < 10 is False.
    00073
    00074 The For statement is a little twisty, so let’s take a more careful look at its operation. In general,
    00075 we can name the parts of the For statement as follows:
    00076
    00077 For (initialization; test; change)
    00078   statement
    00079
    00080 where statement can be a single statement (terminated by a semicolon) or a compound statement
    00081 (enclosed with curly braces). The For statement first executes the initialization code, then
    00082 evaluates the boolean expression test. If the value of test is False, the For statement terminates.
    00083 If the value of test is True, the statement is executed, then the change expression is executed,
    00084 and finally the test is evaluated again. If the value of test is False, the For statement terminates.
    00085 If the value of test is True, the cycle repeats again and again until the value of test is False.
    00086
    00087 As a convenience, it is possible to define and set the value of the initialization variable
    00088 in one step, so the preceding example could have been written
    00089 *****************************************************************************/
    00090
    00091 For (Integer i = 0; i < 10; i = i + 1)
    00092         Print(i);
    00093
    00094 }}
    00095
    00097 /* $Revision: 1.1 $ $Date: 2006/09/08 19:11:31 $ $Author: dgl $ $Name:  $ $Id: _b0118_8cpp-source.html,v 1.1 2006/09/08 19:11:31 dgl Exp $ */
    00098 // The Musimat Tutorial © 2006 Gareth Loy
    00099 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00100 // and published exclusively by The MIT Press.
    00101 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00102 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00103 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00104 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00105 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00106 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00107 // The Musimat website:                 http://www.musimat.com/
    00108 // This program is released under the terms of the GNU General Public License
    00109 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00110