11.8. Otherwise

| 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:
> ...
> 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)
or: otherwise unless (a condition)
or: otherwise (a phrase)
or: else if (a condition)
or: else unless (a condition)
or: 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: ... otherwise: ... 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 (word 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.

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

> "Numberless" The Rambling Warren is a room. When play begins: let N be a random number between 1 and 5; if N is 1: say "N is one."; otherwise if N is 2: say "N is two."; otherwise if N is 3: say "N is three."; otherwise: say "N is more than the number of your toes." The final "otherwise" here will fire only if none of the earlier conditions applies; we could leave it out and print nothing in the case that N is 4 or 5. The more compact way to do this is to create a list of values that our number could match; in many programming languages this is called a switch statement. For example: When play begins: let Y be a random number between 6 and 10; if Y is: -- 6: say "Six is the magic number!"; -- 7: say "The number of the day is seven!"; -- otherwise: say "Today's magic number is boring." As a final option, we can use a construction we've seen only briefly before now: a table. The use of tables will be explained more fully in their own chapter, but here we see in brief that we can assign a number of values to one column of a table and then use that table to look up output: When play begins: let X be a random number between 11 and 14; if X is a number listed in the Table of Switching, say "[output entry][paragraph break]"; otherwise say "X is greater than the number of your noses!" Table of Switching
> number | output
> 11 | "X is eleven!"
> 12 | "X is twelve!"
> 13 | "X is thirteen!"
Test me with "z". As we shall see, things other than text can be stored in tables, so we could also use a table as a way to look up objects or even rules to carry out.

Previous | Contents | Next