Suppose you had a program that could do simple calculations. The user will enter two numbers and then they will have the choice of what operation to perform on those numbers. They can add, subtract, multiply, divide or make a power out of them. Here's the code:
|
Enter first number 5 Enter second number 2 What do you want to do with the numbers? (A)dd them (D)ivide them (M)ultiply them make a (P)ower out of them (S)ubtract them D 2.5 |
This is not a very user friendly program. The user must type upper case letters for the menu to work properly. Here is a way to make the case statement more user friendly:
|
The last example also shows how you can have as many statements as you like for each case. If the user enters P or p for their choice then all three of those put statements will be executed. Here is a sample run of the program:
Enter first number 2 Enter second number 5 What do you want to do with the numbers? (A)dd them (D)ivide them (M)ultiply them make a (P)ower out of them (S)ubtract them p 2 to the 5 is 32 |
|