CSC 270 - Survey of Programming Languages: Scheme (Racket)
Conditional Expressions and Programs
Booleans and Relations
- Computer programs have to be able to handle different sets
of data in different ways.
- A game has to determine if a player is within a certain part
of the screen.
- Employees for XYZ & Co.may work between 12 and 65 hours. Any
number outside this range is not valid.
Conditions
- Conditions are mathematical statements that are either true
or false.
E.g., a number is either equal, less than or greater than another number.
x = y x is equal to y
x > y x is greater than y
x < y x is less than y
- These statements are either true or false.
- If x = 4 and y=5. which of these statements are true?
Which are false?
Compound Conditions
Conditions in Scheme
Programs That Test Conditions
- The following program tests whether a number is 5
;; is 5 : number --> boolean
;; to determine whether n is equal to 5
(define (is5 n)
( = n 5))
- The following program tests whether a number is between 5
& 6:
;; is-between-5-6 : number --> boolean
;; to determine whether n is between 5 and 6 ;;(exclusive)
(define (is-between-5-6 n)
(and ( < 5 n) (< n 6)))
- The following program tests whether a number is between 5 & 6
or over 10:
;; is-between-5-6-or-over-10 : number --> boolean
;; to determine whether n is between 5 and 6 ;;(exclusive) or greather than or equal to 10
(define (is-between-5-6-or-over-10 n)
(or (and ( < 5 n) (< n 6))
(>= n 10)))
Conditional Programs
Designing Conditional Programs
- Example -a bank pays higher interestrates to depositors with larger balances:
over $10,000 6%
over $5000 and up to $10,000 5.5%
over $1000 and up to $5000 4.5%
up to $1000 4%
- How do we write this in Scheme?
(cond
[(<= n 1000) .040]
[(<= n 5000) .045]
[(<= n 10000) .055]
[( > n 10000) .060])
- This expression allows us to consider bad data, where we return an interest rate of 0.0
(cond
[(<= n 1000) .040]
[(<= n 5000) .045]
[(<= n 10000) .055]
[( > n 10000) .060]
[else 0.0])
- How do we use this in a program
(define (interest-rate amount)
(cond
[(<= amount 1000) .040]
[(<= amount 5000) .045]
[(<= amount 10000) .055]
[( > amount 10000) .060]
[else 0.0]))
Designing Conditional Programs
- Developing conditional programs are more complicated. It
requires that the problem statement list the different cases.
- The necessary steps in developing conditional programs:
- Data Analysis - Identifying all the various data
situations
- Program Examples - to illustrate the different
situations
- The Program Body
- Conditions - expressing the situation as
conditional expressions
- Answers - what happens in this
case.
- Simplification - is there a simpler way to state the same thing that is
correct?
Data Analysis
Program Examples
- Our interest rates are in the 0-1000, 1000-5000 and 5000+
ranges.
- We pick numbers in each range so that we can run test cases.
- We'll pick 500, 2000 and 7000 as examples.
Program Body - Conditions
- The general framework is:
(define (interest-rate amount)
(cond
[....]
[....]
[....]))
- The conditions are:
(and (<= 0 amount) (<= amount 1000))
(and (< 1000 amount) (<= amount 5000))
(> amount 5000)
- Our program becomes:
(define (interest-rate amount)
(cond
[(and (<= 0 amount) (<= amount 1000))...]
[(and (< 1000 amount) (<= amount 5000))...]
[(> amount 5000)...]))
Program Body - Answers
Simplication
[Back to the Notes Index]