1 / 12

Designing Programs with Branches

Designing Programs with Branches. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. Tax Example. The program must ask for income and number of dependents.

belva
Download Presentation

Designing Programs with Branches

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 ifwith 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. 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

  7. Pseudocode • Prompt for income, dependents • Validate income and dependents If legal: • Compute deductions and taxable income • Branch on taxable income • Income <= 20000 • Income <= 40000 • Else Income > 40000 • Compute tax • If tax < 0, tax = 0 • Print tax Otherwise if dependents < 0, error message • If income < 0, error message • If dependents < 0, error message

  8. Incremental Development • Can build and test program one branch at a time • Usually start with outerbranches 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 • …

  9. Tax Program version 1 • Just branches for validation • Temporary message for legal branch • Test cases: income < 0, deductions < 0, legal values

  10. Tax Program version 2 • Add branches for tax brackets, messages for each • Test cases: taxable income for each branch

  11. 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

  12. Tax Program, version 4 • Add code to check for negative taxes • Test cases: Taxes > 0, taxes < 0

More Related