Multibranch if-else Statements

Notice how the value of balance in Listing 8.2 determines which of the five different print statements the program will execute. Letting the value of a variable determine which statement (or compound statement) to execute of several possible alternatives is so commonplace in the programming world that a standardized system has been devised for writing the nested if-else structures used to implement this functionality. The resulting structures are called multibranch if-else statements.

Let's have a look at how we can turn Listing 8.2 into this standard style. We start by changing the flowchart of Figure 8.10 to form a new flowchart that yields the same results but follows a couple of standard conventions:

  • Only the ...

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.