4.4. Using Flags

A flag is simply a variable that contains the Boolean value true or false. This value is usually inverted (or flipped) during program execution based on some event occurring. For example, you might have a flag called endOfData that is used to keep track of whether the last data item has been processed. Initially, you set this flag false, using a statement like this:

endOfData = false

You might also have a statement that executes in a loop as long as you haven't reached the end of your data. You write such a loop this way:

repeat while not endOfData
   -- process the data
end repeat

Presumably, somewhere inside the repeat while statement a test appears to see if the end of data is reached. This is followed by a statement to set the flag true, like this:

endOfData = true    == signal we've reached the end

After this flag is set true, the expression not endofData at the top of the repeat while evaluates false. This causes the repeat while statement to terminate.

4.4.1. Another Look at the Triangular Number Program

Next, you revisit the triangular number program and modify it to handle the situation in which no valid integer value is entered when prompted. In the previous chapter, you saw how to catch such an error by using the try statement. In the following Try It Out, however, if you type a bad number, you get another chance. In fact, you keep getting the same dialog displayed over and over until you finally enter a number or click Cancel to end the program.

4.4.1.1. ...

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.