0% found this document useful (0 votes)
4 views1 page

Understanding While and Do Loops in Mathematica

Uploaded by

designer.mar.44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Understanding While and Do Loops in Mathematica

Uploaded by

designer.mar.44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

28 Chapter00.

nb

2
6
38
1446
2 090 918
Let's look at that example carefully. First, we assigned the value 2 to the symbol i. This is similar to
the initialization in a For loop, but must take place outside the structure of the While expression.
This should be viewed as adding flexibility, since in many cases the initialization process itself can be
quite involved. The first argument is the conditional statement that controls the loop. This can be any
condition you want. The condition is followed by the body of the loop. The statement sequence is
executed repeatedly until the condition is false.
In our example, there are two expressions in the body argument. First, the current value of i is printed.
Then the value of i is changed to the result of squaring it and adding 2. This continues as long as the
value of i is less than 107 .
It is very important in a While loop to be sure that the body of the loop will eventually have the effect
of making the controlling condition false. Think about what would happen if the second expression in
the body of the previous loop had been i = i - 2. In that case, i would have started out equal to 2.
Then it would become 0, then -2, then -4, then -6, etc, and it would never exceed 107 , so it would never
cease – an infinite loop. It requires great care to avoid creating infinite loops in While loops, even
more so than with For loops.
Do loops
A third kind of loop in Mathematica is the Do loop. While the Do loop is less flexible than While, its
syntax and unique semantics make it very useful.
A Do expression requires two arguments, but unlike both While and For, the body of the loop is the
first argument. The second argument defines a loop variable and specifies its iteration using the same
syntax as the Table function. In fact, Do and Table operate in very similar ways, the difference
being in the output. Where Table builds a list from the results of evaluating its body, Do should be
thought of as merely executing the commands in its body and outputs Null, just as For and While.
For reference, we repeat the table of allowed iteration specifications from above.
8count< count copies
8i, max< i ranges from 1 to max
8i, min, max< i ranges from min to max
8i, min, max, step< i ranges from min to max by step
8i, list< i ranges over elements of list
Below, we use a Do loop to print the squares of a list of integers.

You might also like