Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1
Tax Example The program must ask for income and number of dependents. Deductions are computed as a standard deduction of $5000 plus $2000 per dependent, which is subtracted from the income to get taxable income. There are three tax brackets based on taxable income – No more than $20,000 tax rate = 10% of taxable income – More than $20,000 and up to $40,000 tax rate = $ % of taxable income over $20,000 – Above $40,000 tax rate = $ % of taxable income over $40,000 The tax may not be less than $0 (if it is, than it should be set to $0). Neither the income nor the number of dependents may be less than 0.
Analysis What requires a branch? – What is it based on? – How many branches? – What to do down each branch? What order must branches be in? – What must be decided first? – What other decisions will be based on that decision?
Analysis of Tax Brackets “There are three tax brackets based on taxable income” – Based on income and deductions, so must get these values and compute taxable income first – Three branches with three ranges <= <= Above Can do with elif
Analysis of Tax Limit “The tax may not be less than $0 (if it is, than it should be set to $0).” – If tax < 0, tax = 0 Otherwise tax unchanged, so can be if with no else – Based on tax, so must be after tax bracket Done for all tax brackets, so must be afterwards instead of nested in each branch
Validation Making sure input to program “legal” before running code using that input – Customer can help determine what is “legal” – Often prevents program from crashing Structure: code to get input if input not legal: error message else: rest of program possibly with other conditions
Analysis of Validation “Neither the income nor the number of dependents may be less than 0.” Three possibilities: – Income < 0 error message – Dependents < 0 error message – Both legal Do rest of calculations Depends on income, dependents Must be done before other branches – All other branches nested inside legal branch
Pseudocode 1.Prompt for income, dependents 2.Validate income and dependents If legal: 1.Compute deductions and taxable income 2.Branch on taxable income 1.Income <= Income <= Else Income > Compute tax 4.If tax < 0, tax = 0 5.Print tax Otherwise if dependents < 0, error message 1.If income < 0, error message 2.If dependents < 0, error message
Incremental Development Can build and test program one branch at a time – Usually start with outer branches first – Can print diagnostic message down each branch first for testing – Run all test cases for that branching – Build code for one branch at a time –…–…
Tax Program version 1 Just branches for validation Temporary message for legal branch Test cases: income < 0, deductions < 0, legal values
Tax Program version 2 Add branches for tax brackets, messages for each Test cases: taxable income for each branch
Tax Program Version 3 Add code for each tax bracket (can do one at a time) Add code to compute and print taxes for test cases
Tax Program, version 4 Add code to check for negative taxes Test cases: Taxes > 0, taxes < 0