Conditionals - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Conditionals

Description:

Calculating pass - fail. Consider the task of assigning a pass or fail: ... 'fail ) ) Don t worry about these symbols for now. Don t worry about these symbols for now ... – PowerPoint PPT presentation

Number of Views:124
Avg rating:3.0/5.0
Slides: 22
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Conditionals


1
Conditionals
2
Outline
  • Prerequisites
  • Basic functions
  • Boolean Expressions
  • Objectives
  • (if )
  • (cond )
  • Designing conditional functions
  • Reference
  • HTDP Sections 4.3, 4.4

3
Background
  • We frequently need to make choices depending on
    the value of data
  • e.g. if we were looking for only real roots of a
    quadratic, we might test the discriminant for
    being negative
  • Choices will use boolean expressions to determine
    the appropriate result
  • We will use the (if ) or(cond ) function to
    make these choices
  • Warning There may be some deliberate errors in
    the code used in examples today

4
The if function
  • General form of (if )
  • (if ()
  • ()
  • ())

The condition is a boolean expression
Each action is either a literal or a function to
be evaluated
Each action is either a literal or a function to
be evaluated
5
Calculating pass - fail
  • Consider the task of assigning a pass or fail
  • pass-fail number - symbol
  • to determine pass or fail for a given
  • numerical grade
  • examples
  • 80 - pass
  • under 80 - fail
  • (define (pass-fail grade)
  • (if ( grade 80)
  • 'pass
  • 'fail ) )

6
The cond function
  • General form of (cond )
  • (cond
  • else )

Each condition is a boolean expression
Each result is either a literal or a function to
be evaluated
A catch-all else condition is optional but
highly recommended
may be used instead of ( ) wherever
they improve readability
7
Calculating a grade
  • Consider the task of assigning a letter grade
  • letter-grade number - symbol
  • to determine the letter grade for a given
  • numerical grade
  • examples
  • 90 - A
  • under 60 - F
  • (define (letter-grade grade)
  • (cond ( grade 90) 'A
  • ( grade 80) 'B
  • ( grade 70) 'C
  • ( grade 60) 'D
  • else 'F))

8
Designing Conditional Functions
  • We will follow an outline similar to that for
    designing ordinary functions.
  • We add a new step Data Analysis and Definition
  • We elaborate the process of developing the
    function body to allow for the complexity of the
    (cond ) function.

9
Still the Same Questions to Answer
  • Problem statement
  • What is relevant?
  • What can I safely ignore?
  • The Program
  • What does it consume? its inputs
  • What does it produce? its output
  • How are the inputs and outputs related? its
    body
  • Resources
  • Does Scheme provide the functions I need?
  • If not, must I develop those functions myself?
  • Testing
  • How will I know this thing is doing what I want?

10
Programs Purpose (different problem)
  • Begin every program with a CONTRACT which states
    its purpose, first logically
  • interest-rate number - number
  • Generate a program header
  • (define (interest-rate amount) )
  • Write a brief explanatory statement
  • to determine the interest rate for a
  • given amount of money
  • see HTDP Page 39 for the problem statement

11
New Step Data Analysis and Definition
  • Draw a picture of the situation
  • 4 4.5
    5



(
(

5,000
10,000
0
to determine the interest rate for a
given amount of money if amount 4 if amount
5,000, use 5
12
Program Examples (unchanged)
  • Do this before you write the body
  • Predict the behavior of the program
  • Think about the computations involved
  • Illustrate the purpose statement with examples
  • to determine the interest rate for a
  • given amount of money
  • if amount
  • if amount
  • if amount 5,000, use 5
  • test with -1, 700, 1000, 2000, 5000,
  • 7000, 9999, 10000, 12000

13
Write the Body (expanded)
  • Replace the ellipses () in the header with the
    code necessary to implement the computation
  • Add the (cond ) function to the header
  • (define (interest-rate amount)
  • (cond
  • else )

14
Write the Body (expanded)
  • Define each condition and insert it in the body
  • (define (interest-rate amount)
  • (cond (
  • (and ( amount 1000) (
  • ( amount 5000)
  • else )
  • Simplify the expressions
  • (define (interest-rate amount)
  • (cond (
  • (
  • ( amount 5000)
  • else )

15
Write the Body (expanded)
  • Insert the proper results
  • (define (interest-rate amount)
  • (cond (
  • (
  • ( amount 5000) 5.0
  • else 0.0))
  • Pop Quiz what could be used instead of a
    literal as the result?
  • Test the behavior
  • Is this going to work properly?
  • Constant
  • Built-in or user-defined function
  • Sequence of functions
  • (returns the result of the last function)

16
Design the Tests (expanded)
  • As a minimum, ensure that the examples work
  • Cannot show correct behavior for all inputs
  • Look for difficult logical situations
  • Mathematical (could I divide by 0?)
  • Logical (test all paths)
  • i.e. make sure each condition is tested
  • Check normal operation, and both upper and lower
    extremes
  • Large numbers
  • Small numbers

17
Demo
18
Actual Code
  • to determine the interest rate for a
  • given amount of money
  • if amount
  • if amount
  • if amount 5,000, use 5
  • test with -1, 700, 1000, 2000, 5000,
  • 7000, 9999, 10000, 12000
  • (define (interest-rate amount)
  • (cond (
  • (
  • (
  • else 5.0))

19
Questions?
20
Summary
  • You should now know
  • How to use (if )
  • How to use (cond )
  • Designing conditional functions

21
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com