Inform 7 Home Page / Documentation

§11.8. Otherwise

We often need code which does one thing in one circumstance, and another the rest of the time. We could do this like so:

if N is 2:
if N is not 2:

but this is not very elegant, and besides, what if the action we take when N is 2 changes N so that it becomes something else?

Instead we use "otherwise":

otherwise if (a condition)


otherwise unless (a condition)


otherwise (a phrase)


else if (a condition)


else unless (a condition)


else (a phrase)

This phrase can only be used as part of an "if ...:" or "unless: ...", and provides an alternative block of phrases to follow if the first block isn't followed. Example:

if N is 2:

When there is only a single phrase we can use the shortened form:

if N is 2, say "Hooray, N is 2!";
otherwise say "Boo, N is not 2...";

We can also supply an alternative condition:

if N is 1:
otherwise if N is 2:
otherwise if N is greater than 4:

At most one of the "..." clauses is ever reached - the first which works out.

If the chain of conditions being tried consists of checking the same value over and over, we can use a convenient abbreviated form:

if (value) is:

This phrase switches between a variety of possible blocks of phrases to follow, depending on the value given. Example:

if the dangerous item is:
    -- the electric hairbrush:
        say "Mind your head.";
    -- the silver spoon:
        say "Steer clear of the cutlery drawer."

One alternative is allowed to be "otherwise", which is used only if none of the other cases apply, and which therefore guarantees that in any situation exactly one of the blocks will be followed.

if N is:
    -- 1: say "1.";
    -- 2: say "2.";
    -- otherwise: say "Neither 1 nor 2.";

This form of "if" layout is not allowed to use "begin" and "end" instead of indentation: it would look too messy, and would scarcely be an abbreviation. It is also not allowed to use "unless" instead of "if", because the result would be too tangled to follow.

arrow-up.png Start of Chapter 11: Phrases
arrow-left.png Back to §11.7. Begin and end
arrow-right.png Onward to §11.9. While

A simple exercise in printing the names of random numbers, comparing the use of "otherwise if...", a switch statement, or a table-based alternative.