100 likes | 178 Views
Learn how to create a simple menu using a switch statement in C programming. Implement a basic calculator with addition, subtraction, multiplication, and division functionalities. Practice creating a user-friendly menu interface for ATM transactions.
E N D
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.
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:
Create a Menu - Simple Calculator • Formulate the algorithm • Which loop control structure should be used? • Counter-controlled repetition • Sentinel-controlled repetition Sentinel-controlled repetition
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
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;
Print out the menu C Code
break statement makes program skip to end of switch switch statement checks each of its nested cases for a match C Code
default case occurs if none of the cases are matched Repeat the menu for each iteration C Code
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