lecture 7: menus and getting input. switch multiple-selection statement switch useful when a...
DESCRIPTION
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:TRANSCRIPT
![Page 1: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/1.jpg)
Lecture 7: Menus and getting input
![Page 2: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/2.jpg)
switch Multiple-selection Statementswitch
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, …
FormatSeries 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 cases 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.
![Page 3: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/3.jpg)
Create a Menu - Simple CalculatorProblemCreate a menu that either adds, subtracts, multiplies or divides two user-entered numbers.
Assume only using integersLoop until the user selects “exit.” Keep printing the menu after each selection.
Formulate the algorithmWhat does the menu look like?
AdditionSubtractionMultiplicationDivisionExit
Please select an operation:
![Page 4: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/4.jpg)
Create a Menu - Simple CalculatorFormulate the algorithm
Which loop control structure should be used?
Counter-controlled repetitionSentinel-controlled repetition Sentinel-controlled repetition
![Page 5: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/5.jpg)
Create a Menu - Simple CalculatorFormulate the algorithm
Pseudocode algorithmPrint the calculator menuInput the operationWhile the user has not as yet entered the sentinel If the operation is:
addition: input two numbers (operands)add two numbersprint the result
subtraction: input two numbers (operands)subtract the second number from the first
oneprint the result
multiplication: input two numbers (operands)multiply the two numbersprint the result
division: input two numbers (operands)divide the first number by the second oneprint the result
exit: no actiondefault: print “Invalid operation! Please select your
operation.”
Print the calculator menu Input the operation
![Page 6: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/6.jpg)
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 Subtractiontrue Input two numbers;Perform subtraction;Print result;
break
false
case multiplicationtrue 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 message break
false
![Page 7: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/7.jpg)
C Code
Print out the menu
![Page 8: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/8.jpg)
C Codeswitch statement
checks each of its nested cases for a match
break statement makes program skip to end of switch
![Page 9: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/9.jpg)
C Code
Repeat the menu for each iteration
default case occurs if none of the cases are matched
![Page 10: 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](https://reader036.vdocument.in/reader036/viewer/2022082908/5a4d1b4b7f8b9ab0599a57d6/html5/thumbnails/10.jpg)
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