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. 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. Vocabulary: case default switch Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 In the IF miniLesson, we considered the problem below: 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: Credit Status Interest Rate A 5% B 10% C 12% 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) ©Christine S. Wolfe 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. main() GET CreditStatus GET Total Merchandise IF CreditStatus == 'A' N A B IF CreditStatus == 'B' IF CreditStatus == 'C' N Y Y Interest = TotalMerchandise * 0.10 Interest = TotalMerchandise * 0.12 B PRINT Interest Y Interest = TotalMerchandise * 0.05 A STOP N 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. 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. 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. START GET CreditStatus GET TotalMerchandise if CreditStatus == 'A' CASE CreditStatus CALCULATE case 'A': Interest = TotalMerchandise * 0.05 end if CALCULATE Interest = TotalMerchandise * 0.05 if CreditStatus == 'B' case 'B': CALCULATE Interest = TotalMerchandise * 0.10 CALCULATE Interest = TotalMerchandise * 0.10 end if case 'C': if CreditStatus == CALCULATE 'C' Interest = TotalMerchandise * 0.12 CALCULATE Interest = TotalMerchandise * 0.12 end case end if 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. 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 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 START STOP 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 Flowchart for Customer Interest Payment C. S. Wolfe 01-Aug-2008 START A B GET CreditStatus CASE OF CreditStatus PRINT Interest 'A' GET Total Merchandice 'B' A 'C' B Interest = TotalMerchandice * 0.05 Interest = TotalMerchandice * 0.10 Interest = TotalMerchandice * 0.12 STOP Flowcharting a CASE (aka SWITCH) statement Preferred method Alternative method CASE OF variable value1 value2 value3 default statements executed when variable == value1 statements executed when variable == value2 statements executed when variable == value3 statements executed when variable != any value listed CASE OF variable value1 value2 value3 default statements executed when variable == value1 statements executed when variable == value2 statements executed when variable == value3 statements executed when variable != any value listed
© Copyright 2026 Paperzz