11.9. While

| Chapter 11: Phrases |

| 11.9. While

The next control phrase is "while", which has the form:

while (a condition):

This phrase causes the block of phrases following it to be repeated over and over for as long the condition is true. If it isn't even true the first time, the block is skipped over and nothing happens. Example: while someone (called the victim) is in the Crypt: say "A bolt of lightning strikes [the victim]!"; now the victim is in the Afterlife;

We must be careful not to commit mistakes like the following:

> while eggs is eggs:
> say "again and ";

which, as sure as eggs is eggs (which is very sure indeed), writes out

> again and again and again and again and again and ...

forever. (Inform won't prevent this: we will find out the hard way when the game is played.) While we would probably never write anything so blatant as that, the mistake is all too easy to commit in disguised form. We should never design a loop, as repetitions like this are called, without worrying about if and when it will finish.

As with "if", we can use "begin" and "end" instead of a tabulated layout if we want to --

> while ...
> begin;
> ...
> end while.

(The "begin" of an "if" must of course match an "end if", not an "end while", and so on.)

| Previous builds allowed two other forms: while (a condition) repeatedly (a phrase) or: while (a condition) , (a phrase)
This phrase causes the single phrase following it to be repeated over and over for as long the condition is true. If it isn't even true the first time, nothing is done at all. These forms of "while" are now deprecated, and should be replaced by the standard block form: while (a condition): ...
Experience shows that it is much more legible to lay out "while" loops as blocks, even in these rare cases when only a single phrase forms the body of the block.

Previous | Contents | Next