Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Slides:



Advertisements
Similar presentations
Programming Example: tax computation. Introduction In this webpage, we will study a programming example using the conditional statements (if and if-else)
Advertisements

Programming Logic and Design Eighth Edition
©Brooks/Cole, 2001 Chapter 5 Selection-- Making Decision.
Repeating Actions While and For Loops
Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Conditional Statements If these were easy then everyone would them!
Making Decisions In Python
Main task -write me a program
CIS101 Introduction to Computing Week 12 Spring 2004.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Adapted from slides by Marie desJardins
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Introduction to Algorithm Design and Documentation CSIS 1595: Fundamentals of Programming and Problem Solving 1.
GCSE ICT Checking data. Why do errors happen? Computers do not make mistakes. However if incorrect data is put in errors happen. In ICT this is called.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 4 Selection Structures: Making Decisions.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
More While Loop Examples CS303E: Elements of Computers and Programming.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSCI-100 Introduction to Computing
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Introduction to Testing CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Controlling Program Flow with Decision Structures.
Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Incremental Testing of Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Structure Based Test Design
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
A451 Theory – 7 Programming 7A, B - Algorithms.
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
How tax is calculated on your Taxable income Example Your gross income = $126,000 Your deduction = $ 6,000 Taxable income = $120,000.
Standard Algorithms Higher Computing.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Selection CIS 40 – Introduction to Programming in Python
Conditions and Ifs BIS1523 – Lecture 8.
Selection-- Making Decision
Lesson 04: Conditionals Class Chat: Attendance: Participation
Intro to Nested Looping
Intro to Nested Looping
Multiple Selections (ELIF Statements)
flow charts and system diagrams
Selection Statements.
Add or Subtract? x =.
Intro to Nested Looping
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
EITC Problem: Single Homework 8: Problem #1.
Chapter 3: Selection Structures: Making Decisions
Intro to Nested Looping
Basic Lessons 5 & 6 Mr. Kalmes.
Presentation transcript:

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