WORDPERFECT MACRO TUTORIAL


15. Loops

15.1 Repeating operations with a loop

There are many situations where you will a macro to perform an action repeatedly, either a certain number of times or until a certain condition exists. For example, you might want to perform the same operations at each occurrence of a specific string in a document, or you might want to print numbered labels. This is made possible by creating a "loop": the commands within the loop are executed over and over again, until the macro exits from the loop.

Caution: It is very easy to forget to provide for an exit from a loop. Such an "endless loop" in a macro will keep executing forever until you manually terminate the macro by pressing the Escape key (or until you run out of memory, or some other external constraint stops the macro). Remember that you must provide for some occurrence that will satisfy the condition that ends the loop.

15.2 Loops with the Go() command

The simplest loop consists of a Label() and a Go() command to jump back to that label. You must provide some test for the macro to exit the loop when that test is met.

Examples:

x :=1
Label(StartOfLoop@)
Type("This is line number ")
Type(x)
HardReturn

x:=x+1 // increment x
If(x>10) Quit EndIf // quit after 10 cycles
Go(StartOfLoop@) // return to start of loop

SearchString("foo")
Label(LoopStart@)
SearchNext()
Type("/bar")
Go(LoopStart@) // macro will end when search fails


These examples are given only for educational purposes; their form is not recommended because Go() commands should be avoided whenever possible, and the condition is better tested within the command than within the loop. The While() and Repeat() commands should be used instead of GO() loops.

15.3 Loops with the While() command

It is easier and more efficient to write loops using the While() command. The While() command tests the condition before the loop is entered, and if the condition is true the commands in the loop are executed. Execution then returns to the While() command, and the condition is tested again. The cycle continues as long as the condition is true.

The form of the While() loop is:

While(condition)
[commands to be executed]
EndWhile

You can see the similarity to If() EndIf.

In order to avoid an endless loop, something must occur in the loop that eventually will cause the condition to be false; the most common examples are moving the cursor, and changing the value of a counter. For example, the following loop could be used to create numbered labels from 1 to 100:

x:=1
While(x<=100)
Type("Label No. ")
Type(x)
HardPageBreak
x:=x+1
EndWhile

If variable x were not incremented, it would never reach 100, and the condition would never become untrue, so the loop would never end.

Sometimes a search is used to position the cursor, and then the cursor is moved to a position before the found text. If this is performed in a loop (to catch each occurrence of the text), the cursor must be moved to a position after the found text before the next iteration through the loop, otherwise the cursor will be positioned at the same occurrence of the text in each pass through the loop, resulting in an endless loop.

This bears repeating: It is very easy to forget to include a step that will finally result in the condition being untrue to end the loop. If a macro seems to run forever, check to make sure you have not inadvertently created an endless loop.

15.4 Loops with the Repeat command

When using the While() command, the condition is tested before the first pass through the loop. This means that if the condition is false at the start, the commands in the loop will not be executed even once. If you want to make certain that the loop is entered at least once, you can have the test performed at the end of the loop by using the Repeat and Until() commands, in the following form:

Repeat
[commands to be executed]
Until(condition)

The commands in the loop will be executed at least once, and will keep being executed until the condition is true. Note that this is the opposite of the While() test, where the loop continues as long as the condition is true,; with Repeat-Until, the loop continues as long as the condition is false.

Other commands that set up loops are For/EndFor, ForNext/EndFor, and ForEach/EndFor. These commands are beyond the scope of this tutorial.




© 1999 Seth H. Katz
All rights reserved


E-MAIL TO AUTHOR


BACK Table of
Contents NEXT