As the output shows, we can simulate rolling a six-sided die by scaling and shifting the values rand
produces. The default
case (lines 41–42) in the switch
should never execute, because the switch
’s controlling expression (face
) always has values in the range 1–6. We provide the default
case as a matter of good practice. After we study arrays in Chapter 7, we show how to replace the entire switch
in Fig. 6.7 elegantly with a single-line statement. This will be another of those “light-bulb” moments.
Error-Prevention Tip 6.4
Provide a default
case in a switch
to catch errors even if you are absolutely, positively certain that ...
Get C++ How to Program, 10/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.