lecture 7: menus and getting input. switch multiple-selection statement switch useful when a...

10
Lecture 7: Menus and getting input

Upload: cuthbert-fleming

Post on 18-Jan-2018

213 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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