The DO Loop Command

Home, Up: Loops

 

On this page:

The DO Command, Inline Loops, Infinite Loops, The MAXLOOP Command, Showing Loop Pass Results, Result File hy.

 

As always, the question mark at the beginning of a line is Hypatia's input prompt, you do not type it.

 

The DO Command

The DO command lets you perform a calculation or call a script in a loop:

? DO n :: calculation or script

n is the number of passes, from 1 to the permitted maximum (one hundred thousand by default, but you can change this limit -- see "The MAXLOOP Command" below).

It has to be a number, not a variable or an expression. You can use scientific notation, and you can also use LAKH, MILLION or CRORE.

The number of passes and the task to be performed (calculation or script) are separated by double colons. A space before and after them is required.

If :: is followed by a calculation, then we have an "inline loop", that is, a loop that is executed within a single line, as opposed to a loop that calls a script.

Script is either the command RUN scriptfile or the short form _scriptfile.

Within a loop RUN scriptfile and _scriptfile are the same (with one exception, see "Showing Loop Pass Results" below), and there is no reason why you should not use the shorter form.

 

Important: Scripts can be used in loops, but a script can not contain a loop. No loop commands are permitted within a script!

Loop commands are not permitted after conditions (which only make sense in loops), that is after I1:, I*:, IF ... THEN, ALSO: and ELSE:.

 

Inline Loops

Loops calling a script are Hypatia's most powerful and versatile feature, but many tasks can also be accomplished with inline loops (though for a task that you have to do repeatedly a script will probably more convenient).

The calculation part of the DO command can be anything that makes sense to run in a loop.

Since a calculation updates $ at each loop pass, at each pass a calculation can build on the previous result.

The calculation can also use the loop index I -- for this, see the next page.

Technically an inline loop could also call a command, though this would make no sense -- one exception would be a variable assignment command.

DO n :: IF ... THEN ... is permitted and can be useful in inline loops.

The IF ... condition can make use of the loop index I (again, see next page).

 

Infinite Loops

An infinite loop is a loop that is meant to run until an exit condition is met. The loop command uses an asterisk instead of a number:

? DO * :: calculation or script

To call a script in an infinite loop there is a short form:

? _*scriptfile

(*_scriptfile instead of _*scriptfile also works. There must be no spaces between the underscore, the asterisk and the file name.)

For an infinite inline loop you have to use DO * :: ..., there is no short form.

 

Infinite inline loops will usually be designed to end when an ITEM operator has exhausted its list of items -- see the page after next.

Even an infinite loop would eventually end if the exit condition is never met -- it would end after the maximum number of loop passes (see below).

In some cases you may actually want the loop to go through this number of passes -- in this case, the asterisk just stands for that number.

 

The MAXLOOP Command

By default, the maximum number of loop passes is one hundred thousand (1e5, or 1 LAKH).

You can change this value with the MAXLOOP command:

MAXLOOP n

where n must be a number (not a variable or an expression) in the range from 10 to ten million.

You can write the number in scientific notation, or you can use LAKH, MILLION and CRORE.

The following lines each set the maximum number of loops to the highest permitted value:

? MAXLOOP 10000000

? MAXLOOP 10'000'000

? MAXLOOP 1e7

? MAXLOOP 10 MILLION

? MAXLOOP 1 CRORE

 

The command MAXLOOP without a number displays the current maximum number of loop passes.

Independent of the currently chosen format, it will always be shown in decimal format with apostrophes.

 

Showing Loop Pass Results

While $ gets updated by every calculation line (and so does $$), by default only the final result is shown after the loop has ended.

If you want the value of $ to be shown after each pass while the loop is performed, you have to add ? to the loop command, after the number of loop passes:

? DO n ? :: calculation

? DO n ? :: RUN scriptfile

? DO n ? :: _scriptfile

(Note that this is not the same as the "debug" question mark at the end of a calculation line -- see page "Debugging a Calculation" in chapter "First Steps".)

You can use DO * instead of a number. You cannot use the short version _*scriptfile, though, you need to use the full DO command.

Only the results of the first 40 passes will be shown. Should you need more, in a script you can use the command =, or you can use accumulation mode to write the results to hy or (preferably) to the result buffer.

 

If logging mode is on, then the results will also be written to the log file.

If your loop calls a script, the results that are shown depend on whether you use RUN filename, or the short version, _filename. With RUN all calculation results in the script will be shown, with _scriptfile only the last result in each loop pass.

Remember that only a calculation line produces a "result" -- assigning a value to a variable does not, even if it involves a calculation. If you want to see values of variables, you have to use the SHOW command.

In a loop with a large number of passes you may want to see, for instance, results only at every tenth pass -- you can do this with the help of the loop index I, which will be discussed on the next page.

 

At the end of a loop the current value of $ will always been shown, even if no calculation has been performed.

 

Result File hy

By default the result file hy gets updated at the end of the script at each pass of the loop, if a calculation has been performed.

Each time, the previous content of hy gets overwritten.

You can modify this behavior by using accumulation mode (see page "Accumulation Mode" in chapter "Results"):

Any calculation line that uses accumulation mode will update hy, adding the result to the previous content -- this means, a script can write several results to hy at each loop pass if several calculation lines use accumulation mode.

When you use accumulation mode, then it has (also) to be used by the last calculation line in the script, or (if hy has been written to by revious lines) the last calculation line could use silent mode -- if the last calculation line uses neither accumulation mode nor silent mode, it will overwrite hy at each loop pass!

 

Home, Up: Loops, Next: Loop Index, Start and End Lines