Inform 7 Home Page / Documentation


§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 story 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.)

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.


arrow-up.png Start of Chapter 11: Phrases
arrow-left.png Back to §11.8. Otherwise
arrow-right.png Onward to §11.10. Repeat