1 / 10

Lecture 7: Menus and getting input

Lecture 7: Menus and getting input. 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. default case occurs if none of the case s are matched.

rafael
Download Presentation

Lecture 7: Menus and getting input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 7: Menus and getting input

  2. 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. default case occurs if none of the cases are matched 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.

  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? • Addition • Subtraction • Multiplication • Division • 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 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. switch statement case division case Addition case Exit operation != Exit begin false end true Print the calculator menu; Input the operation; Input two numbers; Perform addition; Print result; true break false Flowcharting for the simple calculator Input two numbers; Perform subtraction; Print result; true case Subtraction break false Input two numbers; Perform multiplication; Print result; true case multiplication break false Input two numbers; Perform division; Print result; true break false true Print message break false default Print message Print the calculator menu; Input the operation;

  7. Print out the menu C Code

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

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

  10. In-Class Programming Exercise • 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.  Challenge: 4.28

More Related