Twice I’ve touched on looping constructs. Twice I haven’t talked about them. In this post I explore the different looping constructs available in Action!
There are four different types, all centered around the statements DO and OD which mark the beginning and end, respectively, of a loop:
- WHILE
- UNTIL
- FOR
- Infinite
Now I’ll look at how each one is constructed and works.
WHILE Loop
A WHILE loop is constructed using the WHILE statement followed by a conditional, then the DO OD block. Embed the statements you want looped inside the DO OD block. With the WHILE loop, statements within the loop may not execute at all if the condition is already met when the loop is executed. For example, if the condition is x<5, and x=10 at the loop start, the loop won’t be executed because the condition has not been met.
WHILE condition DO ; Some commands OD
UNTIL Loop
An UNTIL loop is constructed a little differently. The UNTIL statement is placed just before the loops closing OD. UNTIL also requires a condition to be met. The biggest difference in execute between UNTIL and WHILE is the statements in the UNTIL loop will aways be execute at least one time. For example, if the condition is x<5 and x=10 at the loop start, the loop will execute once, see the condition has not been met, then exit.
DO ; Some commands UNTIL condition OD
FOR Loop
The FOR loop provides a little more control over the loops execution, mainly in how many times the loop is executed. With WHILE and UNTIL the loop is repeated until a condition is met, which may or may not be a set number of iterations. With FOR, the loops is executed a set number of iterations. You can also control the loop stepping.
FOR counter=n1 to n2 DO ; Some commands OD
If you want to have the loop increment the counter by some number other than 1, you add a STEP clause like this:
FOR counter=n1 to n2 STEP n3
In all of the FOR contracts above, n1 represents the starting number; n2 represents the ending number; and n3 represents the step rate.
Infinite Loop
If you want to repeat a loop forever you can do that too, easily. Just omit the WHILE, UNTIL or FOR completely. The begin and end looping constructs used alone will loop indefinitely. This can be useful to repeat a game loop for instance (check stick movement, check collision, update score, repeat).
DO ; Some commands OD
Stuck in a Loop
Help, I’ve started a loop and can’t get out!
Never fear, you can use the EXIT statement to exit the currently executing loop construct without meeting the terminating condition.
Example Code Breakdown
Here is a small program written to demonstrate the looping constructs.
Start of the program and main routine. One byte (iLp) is declared for the counter. The screen is also prepped for output by setting the left margin to 0 and printing a title:
MODULE PROC Main() BYTE iLp=[0] Poke(82,0) Graphics(0) PutE() PrintE("-[Loops]--------------------------------")
This prints a note about where the counter starts for each loop, then prints a ruler so the progress of each loop can easily be counted:
PrintF("Counter (c) starts at 0.%E%E") PrintE(" 01234567890")
The WHILE loop. The counter is 0 at the start. The loop will execute while the counter (iLp) is less than 10 (in other words 0 through 9). Inside the loop statements a “.” is printed and the counter is incremented by 1. Following the loop end, an EOL is printed:
Print("While c<10 : ") while iLp<10 DO Print(".") iLp==+1 OD PutE()
The UNTIL loop. The counter is reset to 0. The loop will execute until the counter equals 10 (in other words 0 through 9). Inside the loop statements a “.” is printed and the counter is incremented by 1. Following the loop end, an EOL is printed:
iLp=0 Print("Until c=10 : ") DO Print(".") iLp==+1 until iLp=10 OD PutE()
The FOR loop. The counter does not need to be reset. It is initialized to the start value (0) at the loop start. The loop will execute until the counter equals 10 (in other words 0 through 10 – yes 11 times). Inside the loop statements a “.” is printed. Following the loop end, and EOL is printed.
Print("For c=0 to 10 : ") for iLp=0 to 10 DO Print(".") OD PutE()
This tells the user it is about to start the infinite loop, then gets a keystroke which essentially pauses until the user responds. This is done because the printing from inside the infinite loop will eventually push the existing text off the screen as it fills up. The GetD(7) statement is used to get a byte from device 7 which is the keyboard.
PutE() PrintE("Press ANY key for infinite loop") GetD(7)
The Infinite loop. There are no conditions here. The loop will execute forever. Inside the loop statements a “.” is printed.
DO Print(".") OD
This exits the program, though it will never be reached due to the infinite loop immediately prior.
RETURN
Complete Source
Source code listing un-interrupted:
MODULE PROC Main() BYTE iLp=[0] Poke(82,0) Graphics(0) PutE() PrintE("-[Loops]--------------------------------") PrintF("Counter (c) starts at 0.%E%E") PrintE(" 01234567890") Print("While c<10 : ") while iLp<10 DO Print(".") iLp==+1 OD PutE() iLp=0 Print("Until c=10 : ") DO Print(".") iLp==+1 until iLp=10 OD PutE() Print("For c=0 to 10 : ") for iLp=0 to 10 DO Print(".") OD PutE() PutE() PrintE("Press ANY key for infinite loop") GetD(7) DO Print(".") OD RETURN
Results
Next I’ll start looking at file I/O.