Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.

Similar presentations


Presentation on theme: "CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one."— Presentation transcript:

1 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one of several alternatives The switch statement evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements The flow of control transfers to statement associated with the first value that matches

2 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 2 Letter Grade Converter Using Switch Statement #include main(void) { int c, grade; char letterGrade; /* input a percentage grade */ printf("Input a grade in percentage\n"); scanf("%d", &grade); if (grade == 100) c = 9; else c = grade / 10; /* find the corresonding letter grade */ switch (c) { case 9: letterGrade = 'A'; break; case 8: letterGrade = 'B'; break; case 7: letterGrade = 'C'; break; case 6: case 5: letterGrade = 'D'; break; case 4: case 3: case 2: case 1: case 0: letterGrade = 'F'; break; default: printf("The input is an invalid nubmer\n"); } /* output the result */ printf("The corresponding letter grade is %c.\n", letterGrade); }

3 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 3 The Switch Statement Syntax The general syntax of a switch statement switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... default: statement-list } switch Case default are reserved words If expression matches value2, control jumps to here

4 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 4 Switch Statement Expression Type The expression of a switch statement must result in an integral type, meaning an int or a char It cannot be a boolean value, a floating point value a byte, a short, or a long

5 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 5 The break in Switch Statement A break statement is used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case !!!

6 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 6 default in switch statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch

7 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 7 Example of a switch Statement with Type char Case Labels

8 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 8 Case study: design and implement a simple calculator Specifications –it has an dialogue interface for selecting one of the four arithmetic calculation, or quitting the program –For each operation, it gets the operands of float type, compute and display the result –It returns to the interface after each operation. Analysis –Print the interface and prompt for selection –Input the selection 1 or a for addition, 2 or s for substraction, 3 or m for multiplcation, 4 or d for division, 5 or q for quitting the program –If 1 is selected, it prompts for operands a, b, then calculate the sum and display the result. Then go back to the interface again. Similarly to other selection

9 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 9 The Design of a Simple Calculator The main program calls an interface function The interface function –Display a dialogue table –Then get a selection input –using a switch statement to switch different operation –If the selection matches of the case, then call an operator function associated the case, each operator function gets two operands, computes and displays, followed by the calling the interface function. Functions: main(), interface(), sel(), add(), sub(), mul(), div(), quit()

10 CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 10 Algorithm 1.Call interface() to display the interface for selection information 2.Call sel() to take the selection 3.If 1 or a, call add(): 1.get two numbers 2.calculate the sum 3.Display the sum 4.Return 2 4.………… similar to the above See implementation


Download ppt "CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one."

Similar presentations


Ads by Google