Articles Archive
Articles Search
Director Wiki

Repeat Basics

March 4, 1998
by Alex Zavatone

For quite some time, I have been distressed with many portions of the repeat loop functionality. Jumping back to my AppleII and TRS/80 (eeegh) days, it is easy to recall that simple basic for/next loops seemed to have more functionality in the early 80's than our friend the repeat loop does in 1998.

What do I mean by that? For/Next loops had several options that make looping over a value much easier. First was the step command. You didn't have to create a loop that only looped by integer values. The step command allowed you to add or subtract floating point values to your loop variable. Very important in many cases.

The other annoyance is the seperate syntaxes for looping up to a value and looping down to a value in repeat loops. Can anyone tell me why we still have this in 1998? Things like these where the user gets the impression that the use of the command is more difficult than it has to be are annoying because Macromeida has shown release after release that they do not get fixed. Well, thanks to a bit of enlightenment today while reading the crossBasic documentation, I realized that with a small change in how you use repeat loops can fix both of these problems.

We normally use the "repeat with" flavor of the command when looping over values but if you want to loop between 0 and 2 * pi in a repeat loop, it's not obvious how to do this and step at a reasonable interval. If we look at the "repeat while" command, the argument evaluates to a boolean condition. Muuunnnhhh? In english, this means that if the results of the "repeat while" argument = true then the repeat continues where if the results are false, it doesn't. Really pretty simple.

So how do you do it? What's the secret? Don't put your variable in the line where you start the repeat loop. Define it before the loop and incriment or decriment it inside the loop. Then make your condition for the loop to happen the argument to the repeat loop. Muuunnnhhh? Ok, Ok. Here's what it looks like for a simple y = sin(x) loop from 0 to 2*pi stepping by .1

on SinMeBaby
    set x = 0
    repeat while x <= 2*pi
         set y = sin(x)
         put y
         set x = x + .1
    end repeat

and for y = cos(x) loop from 2*pi to -2*pi stepping by - .1

on LoopyCos
    set x = 0
    repeat while x >= -2*pi
         set y = cos(x)
         put y
         set x = x - .1
    end repeat

The boolean conditions here are x <= 2*pi in the first example and x >= -2*pi in the second. We're just saying "keep repeating while my condition is true". Also by iterating our variable x within the loop, we finally get control over the amount x increases and one syntax for looping up and down.

Don't know about you but this simple little discovery made my day. But then where I live, its been raining forever and I get the feeling that most of California will be floating by my window any moment now. :]

- Zav

A Director user since 1987, Alex (Zav) Zavatone has worked on the engineering teams for both Director and Shockwave, 4, 5, 6 and 7. Recent investigations find him developing foundation classes for Director with asynchronous process management and other life threatening activities. In its early days, he had something to do with this Internet thing known as "DOUG". A noted ne'erdowell, slacker and laggard, Mr. Zavatone is nonetheless is trusted by children, small animals and the elderly. In his spare time, Mr. Zavatone rehabilitates lame desert trout.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.