![]() | Chapter 11: Phrases | ![]() ![]() |
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 begin; ...; end if;
if N is not 2 begin; ...; end if;
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":
if N is 2 begin;
...;
otherwise;
...;
end if;
Note that there is only one "end if": the "otherwise" phrase occurs inside the "if ... end if" block of phrases.
When there is only a single phrase we can use the shortened form:
if N is 2 then say "Hooray, N is 2!";
otherwise say "Boo, N is not 2..."
Often, though, what we want is to divide up a situation into many cases, and for this the abbreviation "otherwise if" is allowed:
if N is 1 begin;
...;
otherwise if N is 2;
...;
otherwise if N is greater than 4;
...;
end if;
At most one of the "..." clauses is ever reached - the first which works out. Note that there is only a single "end if", at the end: it all counts as a single "if" construction.
(In almost every computer programming language ever devised, including previous versions of Inform, the word "else" is used rather than "otherwise": for the sake of familiarity, "else" can also be used here. All the same, "otherwise" seems closer to natural English.)
| ![]() A simple exercise in printing the names of random numbers, using "otherwise if..." and also a table-based alternative. (Or, in programming terms, how to emulate a switch statement.) |
|
Previous | Contents | Next |