Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.

Similar presentations


Presentation on theme: "Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can."— Presentation transcript:

1 Lecture 7: Menus and getting input

2 switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can assume and different actions are taken. Creating a menu. ATM, iPhone, … Format Series of case labels and an optional default case switch ( controlling expression ) { case ‘1’: actions case ‘2’: actions default: actions } break - exits from statement. It causes program control to continue with the first statement after the switch statement. default case occurs if none of the case s are matched The switch statement can be used only for testing a constant integral expression. Any combination of character constants and integer constants that evaluates to a constant integer value.

3 Create a Menu - Simple Calculator Problem Create a menu that either adds, subtracts, multiplies or divides two user-entered numbers. Assume only using integers Loop until the user selects “exit.” Keep printing the menu after each selection. Formulate the algorithm What does the menu look like? 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Please select an operation:

4 Create a Menu - Simple Calculator Formulate the algorithm Which loop control structure should be used? Counter-controlled repetition Sentinel-controlled repetition

5 Create a Menu - Simple Calculator Formulate the algorithm Pseudocode algorithm Print the calculator menu Input the operation While the user has not as yet entered the sentinel If the operation is: addition: input two numbers (operands) add two numbers print the result subtraction: input two numbers (operands) subtract the second number from the first one print the result multiplication: input two numbers (operands) multiply the two numbers print the result division: input two numbers (operands) divide the first number by the second one print the result exit: no action default: print “Invalid operation! Please select your operation.” Print the calculator menu Input the operation

6 Flowcharting for the simple calculator begin Print the calculator menu; Input the operation; false end true case Addition true Input two numbers; Perform addition; Print result; break false case Subtraction true Input two numbers; Perform subtraction; Print result; break false case multiplication true Input two numbers; Perform multiplication; Print result; break false case division true Input two numbers; Perform division; Print result; break false default Print message operation != Exit Print the calculator menu; Input the operation; switch statement case Exit true Print messagebreak false

7 C Code Print out the menu

8 C Code switch statement checks each of its nested case s for a match break statement makes program skip to end of switch

9 C Code Repeat the menu for each iteration default case occurs if none of the case s are matched

10 Create a basic ATM menu interface using a switch statement  Create a menu with the following items: 1. Check balance, 2. Deposit, 3. Withdrawal, 4. Quick Withdrawal $25, and 5. Exit. (Hint: These will be the selections for your switch statement.)  Initialize the balance at $100. (Hint: Start your balance variable at 100.)  Each selection should perform it's task within the switch statement.  Loop until the user selects "5. Exit" (keep printing the menu after each selection). Save your file and submit in the ATM Interface dropbox. In-Class Programming Exercise Challenge: 4.28


Download ppt "Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can."

Similar presentations


Ads by Google