Title: CS1321: Introduction to Programming
1CS1321Introduction to Programming
- Georgia Institute of Technology
- College of Computing
- Module 4
- Conditional Expressionsand Cond Statements
2Making the Right choices(HtDP 4)
As fun as it is to write mathematical functions
as Scheme functions, it would be nice to be able
to do something moreinteresting. After all,
computer programs arent built JUST on
mathematical equations. Theyre built on
conditions.
3Conditions
What kind of conditionals?
- Is the fiery demon-monster in line with the
- projectile fired from my BFG-3000?
- Does the plane have enough fuel to be
- put in a holding pattern over the airport?
- Does my car have to increase the amount
- of gas going to the engine to maintain my
- cruising speed of 80 mph?
4Booleans, the building blocks of conditions
Booleans are a new class of values (along with
the numbers and symbols weve already seen).
They represent truth values, and have two
settings
false
true
5Lets start with some old familiar faces
gt, lt,
6Conditional operators
math scheme
result
false true false true false true false false fal
se
4 5 ( 4 5) 5 5 ( 5 5) 5 6 (
5 6)
X Y
resolves to resolves to resolves to
4 lt 5 (lt 4 5) 5 lt 5 (lt 5 5) 5 lt 6 (lt
5 6)
X lt Y
4 gt 5 (gt 4 5) 5 gt 5 (gt 5 5) 5 gt 6 (gt
5 6)
X gt Y
7greater than or equalless than or equal
- Note we also have conditional operators
combining equality with less than or greater
than - lt gt
- (Be sure to type those characters consecutively
- with no space between them.)
8Quick scheme Demo
9Using conditionals to say what we mean
Now we can combine two or three conditions
together. If its raining AND I have some
money, Ill drive down to the Regal and
catch a flick If I have 30 OR someone buys
it off my wishlist, Im going to get the
Clerks Collectors Edition DVD.
10And sometimes we want to negate our
conditional If I do NOT get a loan this
semester, Ill be eating lots of ramen noodles.
11Scheme boolean operators help us do just that
is x between 5 and 10, exclusive? x needs to be
(greater than or equal to 60) or (y needs to be
less than 30) is x not equal to 3?
(and (gt x 5) (lt x 10)) (or (gt x 60) (lt y
30)) (not ( x 3))
12Short-circuit evaluation
When we have numerous conditions the computer may
not evaluate them all. We know some basic things
about and and or
13Run these through the Stepper (and (gt 5
3) (lt 3 5) ( 3 5)) (and ( 3 5) (gt 5 3)
(lt 3 5)) (and (gt 5 3) (lt 3 5) ( 3 3))
(or (gt 3 5) (lt 5 3) ( 3 3)) (or (gt 5
3) (lt 3 5) ( 3 3))
14Functions that Test Conditions
In Scheme lingo, they are called
Predicates
15Predicates
Genuine predicates have the following
properties 1) resolve to true Or
false. (no numbers,
symbols, structures,
strings, hash tables, vectors, function
bodies, etc) 2) name always ends with a
? (with a question mark)
16Examples of predefined predicates
(number? ltvaluegt) is a built in predicate that
tests whether or not ltvaluegt is a
number. (odd? ltvaluegt) is a built in
predicate that returns true if ltvaluegt is
an odd number. false otherwise. (even?
ltvaluegt) is a built in predicate that
returns true if ltvaluegt is an even number.
false
otherwise.
17Examples of predicates you can define
tests if the age indicates this person is a
minor (define (is-minor? age) (lt age
18)) tests if the third number is
between the first and second numbers,
inclusive. (define (between? first second numb)
(and (lt first numb) (lt numb
second))) tests if the second number is an
odd factor of the first (define (odd-factor? numb
factor) (and (odd? factor) ( 0
(remainder numb factor))))
18Using Your Predicates
How do we go about using these boolean-valued
functions (meaning they return booleans) to make
decisions?
We need a conditional statement!
19cond
The built-in function cond allows us to create
our conditional statement. Its format is as
follows
(cond question answer question
answer else answer)
(cond question answer question
answer question answer)
OR
The version on the left describes each category
we expect explicitly.
20condcontinued
What does question answer mean?
question is where you insert a predicate,
(a function that
evaluates to true
or false.) A boolean-valued function.
answer is what will happen if the question
resolved to true
21How many questions and answer pairs can I
have? As many as you need. The
cond statement handles one or more QA
pairs. Whats the deal with else?
else is a built-in catch-all clause for your
cond statement. Its like a question that
is always true. Will this change my
Design Recipe? Yes.
22Example
- Lets design a simple example that uses cond.
- Suppose we have a function that determines the
unit price of a item discounted in quantity.
Given one or more DVDs, the price is - buy 1 DVD for 25.00
- buy 3 DVDs for 20.00 each
- buy 10 or more DVDs for 12.00 each
23Analysis
1
We begin with the Data Analysis section. We know
from the cs1321 template that the section is
called
lt1gt
Data Analysis Definitions
For now, we will do only the data analysis. A
data definition will come later. (Label this
section Data Analysis Definitions
nevertheless.)
24Analysis
So what do we know about the data our function
will use?
lt1gt
Data Analysis Definitions The unit price
of 1 DVD is 25 order 3 or more, the unit price
is 20 order 10 or more, the unit price is 12
Give a short description of the data were using.
We note that the unit price varies with the
quantity of the order.
A number!
25Contract
2
This gives us enough to build a contract
Contract unit-price number --gt number
We have to give the function a quantity, and it
returns the unit price. That is, it consumes a
number and produces a number.
26Purpose
3
This leads us to the purpose
Purpose to return the unit price of given
quantity of DVDs
27Examples
4
Now things get interesting...
Examples (unit-price 1) should return
25 (unit-price 3) should return 20
(unit-price 8) should return 20 (unit-price
10) should return 12 (unit-price 11) should
return 12
Note weve designed an example case for each
condition
28Examples
4
What about zero units? It does not fit the
problem statement.
Now things get interesting...
Examples (unit-price 1) should return
25 (unit-price 3) should return 20
(unit-price 8) should return 20 (unit-price
10) should return 12 (unit-price 11) should
return 12
Note weve designed an example case for each
condition
29Template
5
Coming soon
Template lt not required gt
30Definition
6
Our definition falls out of the Data Analysis
Definition (define (unit-price quantity)
(cond . . . ? . . . Hmm? How many different
conditions do I need?? . . . ? . . .
))
Note that the else could be rewritten with a
specific case. This might make it easier to
later modify the function for new prices/quantity
discounts, etc.
31Definition
6
Our definition falls out of the Data Analysis
Definition (define (unit-price quantity)
(cond . . . ? . . . ? look at your data
analysis it tells you! ? . . . ? .
. . ))
Note that the else could be rewritten with a
specific case. This might make it easier to
later modify the function for new prices/quantity
discounts, etc.
32Definition
6
Our definition falls out of the Data Analysis
Definition (define (unit-price quantity)
(cond (lt quantity 3) 25 (and(gt
quantity 3)(lt quantity 10)) 20 (gt
quantity 10) 12 ))
Note that we use three very specific exact cases.
We are careful to precisely cover the range of
possibilities. This might make it easier to
later modify the function for new prices/quantity
discounts, etc.
33Definition
6
Our definition falls out of the Data Analysis
Definition (define (unit-price quantity)
(cond (lt quantity 3) 25 (and(gt
quantity 3)(lt quantity 10)) 20 else 12
))
Note the else being used this time. This form
is not quite as explicit as the previous. We
recommend the previous way.
34Tests
Tests (unit-price 1) should
return 25 (unit-price 3) should
return 20 (unit-price 8) should
return 20 (unit-price 10) should
return 12 (unit-price 11) should return 12
7
Include tests for each example, and perhaps a few
more...
35Another Styleof Testing
Tests (unit-price 1) 25 (unit-price
3) 20 (unit-price 8) 20 (unit-price
10) 12 (unit-price 11) 12
You can use the approach instead of commented
should return
36Tests
Or even
Tests ( (unit-price 1) 25) ( (unit-price 3)
20) ( (unit-price 99) 12) ( (unit-price 8) 20)
37Tests
We can then see if our tests all return true.
This lets the computer do the tedious chore
checking if numbers are equal.
38Back to Symbols
- A symbol is a sequence of characters that is not
a number - this is a bunch of symbols
- We have seen symbols like sqrt and that have
meaning predefined - We have also seen symbols like sq and hypotenuse
that we define - Some symbols have no meaning attached - we mark
those by preceding the symbol with a quote mark
39Meaning of the Quote Mark
- The quote mark or tick tells the Scheme machine
not to look for the symbol binding just to
accept the symbol a-is. - For example
- gt a
- reference to undefined identifier a
- gt sqrt
- sqrt this primitive operator must be applied to
arguments expected an open parenthesis before
the primitive operator name - gt 'a
- 'a
- gt 'sqrt
- 'sqrt
40Symbols
- Symbols
- are atomic pieces of data
- can use quote to suppress evaluation
- are atomic even when made up of multiple letters
- have a predicate for testing equality between
symbols (symbol? one one) - true
- (symbol? one two)
- false
41Symbols
Just as with numbers, symbols can be returned
from functions we write. We can also pass
symbols into a function. (define (day-of-week
day-num) (cond
( day-num 0) Saturday
( day-num 1) Sunday
( day-num 2) Monday
( day-num 3) Tuesday
( day-num 4) Wednesday
( day-num 5) Thursday
( day-num 6) Friday ))
42Symbols
Whats the contract for this one? (define
(day-of-week day-num) (cond
( day-num 0) Saturday
( day-num 1) Sunday
( day-num 2) Monday
( day-num 3) Tuesday
( day-num 4) Wednesday
( day-num 5) Thursday
( day-num 6) Friday ))
43Symbols
Contract day-of-week number -gt
symbol (define (day-of-week day-num)
(cond ( day-num 0)
Saturday ( day-num 1)
Sunday ( day-num 2)
Monday ( day-num 3)
Tuesday ( day-num 4)
Wednesday ( day-num 5)
Thursday ( day-num 6)
Friday ))
44Strings more later
In DrScheme, you can also define compound pieces
of data called strings. Strings are not atoms of
data like symbols They are made of many, many
parts. this is a string (define name John
Doe) Like symbols, strings are symbolic data.
But thats where the similarity stops. Well use
string later, once we come to understand compound
data.
45Wow! Images
In DrScheme, you can also define images--a third
type of symbolic data. Use the Insert Image
option to load an image.
46Images can you believe it?
The images then become bound to the variable
names we give them. Well work more with
graphics later on.
47Try it!
48Questions?
49(No Transcript)