The switch Statement

The switch statement enables a program to select from multiple actions, based on the value of a controlling expression also called a switch expression. The logic implemented by the switch statement is, therefore, somewhat similar to that of a multibranch if-else statement. To demonstrate the similarities and differences between a switch statement and a multibranch if-else statement, let's have a look at an example.

The program of Listing 8.10 implements the logic behind a “talking” dishwasher by using a multibranch if-else statement. By choosing one of the numbers 1, 2, or 3, the user can select one of the three programs featured by the dishwasher—1: Economy, 2: Intensive, 3: Universal. Every time a choice has been made, ...

Get C# Primer Plus 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.