Download presentation

Presentation is loading. Please wait.

Published byNorma Cubbage Modified over 2 years ago

1
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

2
The CASE logic structure This slide show describes the CASE logic structure, its implemenation in C as the switch statement, and how flowcharts are used to document them. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Vocabulary: case default switch

3
Credit StatusInterest Rate A5 % B10% C12% Example 1: Customer Bob has a credit status of A and orders $1000.00 of merchandise. The interest charged on his order is $50.00 (1000.00 * 0.05) Example 2: Customer Alan has a credit status of C and orders $500.00 of merchandise. The interest charged on his order is $60.00 (500.00 * 0.12) Calculate the interest charged on an invoice. The interest rate is determined by the customer's credit status as determined by the vendor as shown in the chart below: In the IF miniLesson, we considered the problem below: ©Christine S. Wolfe

4
In the earlier example, we developed an algorithm that included a series of IF statements. START GET CreditStatus GET TotalMerchandise if CreditStatus == 'A' CALCULATE Interest = TotalMerchandise * 0.05 end if if CreditStatus == 'B' CALCULATE Interest = TotalMerchandise * 0.10 end if if CreditStatus == 'C' CALCULATE Interest = TotalMerchandise * 0.12 end if PRINT Interest STOP Notice that each if statement compares the variable CreditStatus to one of its valid values.

5
main() IF CreditStatus == 'A' Interest = TotalMerchandise * 0.05 GET CreditStatus GET Total Merchandise PRINT Interest STOP A IF CreditStatus == 'B' Interest = TotalMerchandise * 0.10 IF CreditStatus == 'C' Interest = TotalMerchandise * 0.12 N Y B N Y A N Y B

6
Many programming languages use the CASE logic structure. Your textbook calls it a SWITCH because the word, switch, is the keyword used in ANSI C. Most other languages will use the keyword, case. We will use both terms to mean the exact same type of logic structure. The CASE logic structure is used to execute one set of code out of several options. Like the credit problem above, many solutions involve matching a variable to one of a set of possible values. These can always be solved by a series of IF statements. Each IF statement compares the variable to one of the possible values. A shortcut for this technique is the use of the CASE logic structure.

7
Let's look at how the customer interest problem can be solved with a CASE structure. Here is the IF based solution. Click to see the CASE based solution. CASE CreditStatus case 'A': CALCULATE Interest = TotalMerchandise * 0.05 case 'B': CALCULATE Interest = TotalMerchandise * 0.10 case 'C': CALCULATE Interest = TotalMerchandise * 0.12 end case if CreditStatus == 'A' CALCULATE Interest = TotalMerchandise * 0.05 end if if CreditStatus == 'B' CALCULATE Interest = TotalMerchandise * 0.10 end if if CreditStatus == 'C' CALCULATE Interest = TotalMerchandise * 0.12 end if START GET CreditStatus GET TotalMerchandise PRINT Interest STOP

8
START GET CreditStatus GET TotalMerchandise CASE CreditStatus case 'A': CALCULATE Interest = TotalMerchandise * 0.05 case 'B': CALCULATE Interest = TotalMerchandise * 0.10 case 'C': CALCULATE Interest = TotalMerchandise * 0.12 end case PRINT Interest STOP When execution reaches the case statement, the key variable is compared to the first case clause. If the value of the key variable is equivalent to the value of the case clause, then the instructions after the case are executed. If the values of the key variable and the case clause do NOT match, then the key variable is compared to the next case clause. This process continues until either: 1) a matching case clause is found or 2) no more case clauses exist. If no more case clauses exist then execution looks for a default clause. If a default clause is there, the instructions after the default are executed. In any event, whether a clause is executed or not, execution continues with the first statement after the end of the clause statement.

9
Here are the 2 solutions so you can compare them and see that they give the same result for any given Credit Status. START GET CreditStatus GET TotalMerchandise CASE CreditStatus case 'A': CALCULATE Interest = TotalMerchandise * 0.05 case 'B': CALCULATE Interest = TotalMerchandise * 0.10 case 'C': CALCULATE Interest = TotalMerchandise * 0.12 end case PRINT Interest STOP START GET CreditStatus GET TotalMerchandise if CreditStatus == 'A' CALCULATE Interest = TotalMerchandise * 0.05 end if if CreditStatus == 'B' CALCULATE Interest = TotalMerchandise * 0.10 end if if CreditStatus == 'C' CALCULATE Interest = TotalMerchandise * 0.12 end if PRINT Interest STOP

10
Flowchart for Customer Interest Payment C. S. Wolfe 01-Aug-2008

11
Flowcharting a CASE (aka SWITCH) statement Preferred methodAlternative method

Similar presentations

Presentation is loading. Please wait....

OK

Algorithms and Pseudocode

Algorithms and Pseudocode

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Mit lecture ppt on probability concepts Ppt on pre-ignition procedure Led based message display ppt on tv Ppt on hindu religion map Ppt on marketing plan Ppt on statistics in maths lesson Ppt on the periodic table of elements Ppt on game theory operations research Download ppt on oxidation and reduction definitions Ppt on shell scripting in sql