17. Making the case for the switch Statement

In This Chapter

Testing multiple cases with switch

Combining break with switch

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:

What do you want to do?1. Add New Contact2. Edit Existing Contact3. Call Contact4. Text Contact5. Delete Contact6. Quit the ProgramWhat is your choice?

Note

When you create menus ...

Get C Programming Absolute Beginner’s Guide, Third 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.