E N D
Example: Process Customer Record • A program is required to read a customer’s name, a purchase amount, and a tax code. The tax code has been validated and will be one of those listed on page 43 of the textbook. The program must then compute the sales tax and the total amount due, and print the customer’s name, purchase amount, sales tax, and total amount due. Create a: • Defining diagram • Solution algorithm • Pseudocode algorithm using nested if statements
Process Customer Record Pseudocode Customer Record Pseudocode READ Name, Amount, TaxCode IF TaxCode = 0 THEN SET SalesTax = 0 ELSE IF TaxCode = 1 THEN COMPUTE SalesTax = Amount * 0.03 ELSE IF TaxCode = 2 THEN COMPUTE SalesTax = Amount * 0.05 ELSE COMPUTE SalesTax = Amount * 0.07 ENDIF ENDIF ENDIF COMPUTE Total = Amount + SalesTax PRINT Name, Amount, SalesTax, Total END
The Case Structure • The case control structure in pseudocode is another way of expressing a linear nested IF statement • It is used in pseudocode for two reasons: it can be directly translated into many high-level languages, and it makes the pseudocode easier to write and understand • Nested IFs often look cumbersome in pseudocode and depend on correct structure and indentation for readability
General Form of CASE Statement CASE OF variable value1: Statement Block 1 value2: Statement Block 2 … valuen: Statement Block n Other: Statement Block Other ENDCASE
Example: Process Customer Record (Redo of Previous Example) • A program is required to read a customer’s name, a purchase amount, and a tax code. The tax code has been validated and will be one of the items listed on page 49 of the textbook. The program must then compute the sales tax and the total amount due, and print the customer’s name, purchase amount, sales tax, and total amount due. Create a: • Defining diagram • Solution algorithm • Pseudocode algorithm using a case statement
Process Customer Record Pseudocode Customer Record Pseudocode READ Name, Amount, TaxCode CASE OF TaxCode 0: SET SalesTax = 0 1: COMPUTE SalesTax = Amount * 0.03 2: COMPUTE SalesTax = Amount * 0.05 3: COMPUTE SalesTax = Amount * 0.07 ENDCASE COMPUTE Total = Amount + SalesTax PRINT Name, Amount, SalesTax, Total END