17. How Can I Test Lots of Values?

With the switch Statement

image

The if statement is great for simple testing of data, especially if your data tests have only two or three possibilities. You can use if to test for more than two values, but if you do, you have to nest several if statements inside one another, and that can get confusing and hard to maintain.

Consider for a moment how you execute code based on a user’s response to a menu. A menu is a list of options from which to select, such as this one:

Get Absolute Beginner’s Guide to C, 2nd Edition 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.