Chapter 4. Program Looping

In Chapter 3 you learned how to make decisions in your program and how to alter its execution based on the results of those decisions. In this chapter, you learn about another fundamental feature of computer programming languages: the capability to repeatedly execute a set of statements.

In many instances, you'll want to iterate a group of statements. For example, you may want to print each file from a list of files. Or you may want to rename each file in a certain folder. Finally, you may have iPhoto images stored in an album that you want to resize. Each of these situations requires you to apply your process—whether it's printing, renaming, or resizing—to each file, in turn. This is done through the process of iteration.

The capability to repeatedly execute a group of statements is supported in AppleScript by a statement called, appropriately enough, the repeat statement. This statement takes different forms, which you learn about in this chapter. One form, the repeat...times statement, lets you repeat a set of statements a predetermined number of times. Two other forms, repeat while and repeat until, enable you to repeat a set of statements while a particular condition is satisfied or until a particular condition is satisfied. Finally, you can use the last form, repeat with, to repeatedly execute a set of statements for each element in a list.

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.