11.12 switch Statement
For the Pet Registry web page to be useful in the real world, you’d need to modify it to accommodate more than just two types of pets. The web page’s register function uses an if statement to distinguish between two types—Dog and Hedgehog. To accommodate more pet types, you could use an if statement with lots of else if clauses. But the more elegant solution is to use a switch statement.
The switch statement works similarly to the “if, else if” form of the if statement in that it allows you to follow one of several paths. But a key difference between the switch statement and the “if, else if” statement is that the switch statement’s determination of which path to take is based on a single value. (For an “if, else if” ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access