Team LiB   Previous Section   Next Section
repeat {integer} times end [repeat]

Syntax

Repeat 10 times

   Display dialog contriteStatement

End repeat

Description

This loop statement begins with the reserved word repeat, followed by an integer representing the number of times the loop should cycle, then the reserved word times and an end repeat. (The repeat of end repeat is optional.) You can use this variation of repeat if you do not need the finesse of the two more complex but powerful repeat constructs, such as:

repeat with loopVar in list

Once this loop has executed its enclosed script statements integer number of times, it terminates and the script execution resumes after the end repeat. You can also short-circuit this repeat loop by using the exit or exit repeat statement. This causes the script flow to proceed to after end repeat, regardless of whether the loop has cycled integer number of times.

Examples

The following code does exactly what the last repeat example did; it works with each word in a list, finally displaying each of them on a different line. However, it uses the "repeat {integer} times" variation instead. The example also shows that you can use the return value of an expression for "{integer}" including an integer variable, instead of just a literal integer such as 9:

set theString to "Each word on a different line"

set theList to words of theString

set len to length of theList

set displayString to ""

set counter to 0

repeat len times -- len resolves to the length of the word list

   set counter to counter + 1

   set displayString to displayString & return & (item counter of¬ 

   theList)

end repeat

display dialog displayString

In this example, the line repeat len times uses the len variable's integer value to specify how many times the repeat loop should execute. len represents the length of the list of words that the code reassembles into another string.

    Team LiB   Previous Section   Next Section