Download presentation
Presentation is loading. Please wait.
Published byOsborn Caldwell Modified over 9 years ago
1
Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1
2
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 = $2000 + 15% of taxable income over $20,000 – Above $40,000 tax rate = $5000 + 20% 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.
3
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?
4
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 <= 20000 <= 40000 Above 40000 Can do with elif
5
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
6
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
7
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
8
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 <= 20000 2.Income <= 40000 3.Else Income > 40000 3.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
9
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 –…–…
10
Tax Program version 1 Just branches for validation Temporary message for legal branch Test cases: income < 0, deductions < 0, legal values
11
Tax Program version 2 Add branches for tax brackets, messages for each Test cases: taxable income for each branch
12
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
13
Tax Program, version 4 Add code to check for negative taxes Test cases: Taxes > 0, taxes < 0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.