4.5. The repeat until statement

This form of the repeat statement is actually quite similar to the repeat while. Whereas the repeat while statement repeats execution of a set of statements while a condition is true, the repeat until repeats execution of a set of statements while a condition is false, or in other words, until it becomes true.

The general format of the repeat until statement is

repeat until Boolean-expression
    statement
    statement
    ...
end repeat

You could replace the repeat while from the second version of the triangular number program with the following equivalent:

repeat until validNumber
    ...
end repeat

And taking note of the inverted logic here, you could replace the repeat while from the final version of the triangular number program with the following:

repeat until tries > 3 or validNumber
    ...
end repeat

You've seen three different uses of the repeat statement to count to 10. Here's another example using the repeat until:

set n to 1
repeat until n > 10
    log n
    set n to n + 1
end repeat

As previously noted, you often have a choice of which form of the repeat statement to use to accomplish a given task. If you're going to execute a set of statements a specified number of times, the repeat...times is a logical choice. And if you need access to a variable that counts the repetition number along the way, the repeat with-from-to statement usually fits the bill.

In the following Try It Out, you write a simple guessing program and learn about a special command for generating ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.