Numberless
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:
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:
number | output |
11 | "X is eleven!" |
12 | "X is twelve!" |
13 | "X is thirteen!" |
Test me with "z".
>(Testing.)
>[1] z
Time passes.
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.