csc 160 computer programming for non-majors lecture #10: conditionals i prof. adam m. wittenstein...

36
CSC 160 CSC 160 Computer Programming Computer Programming for Non-Majors for Non-Majors Lecture #10: Conditionals Lecture #10: Conditionals I I Prof. Adam M. Wittenstein Prof. Adam M. Wittenstein [email protected] [email protected] http://www.adelphi.edu/~wittensa/csc160/ http://www.adelphi.edu/~wittensa/csc160/

Post on 21-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

CSC 160CSC 160Computer Computer

ProgrammingProgrammingfor Non-Majorsfor Non-Majors

Lecture #10: Conditionals ILecture #10: Conditionals I

Prof. Adam M. WittensteinProf. Adam M. Wittenstein

[email protected]@adelphi.edu

http://www.adelphi.edu/~wittensa/csc160/http://www.adelphi.edu/~wittensa/csc160/

Page 2: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

CW5: part 1 on BooleansCW5: part 1 on Booleans Complete Exercise 4.1.2 from Complete Exercise 4.1.2 from How to Design How to Design

Programs without using DrScheme.Programs without using DrScheme. Feel free to use a calculator - there is one on Feel free to use a calculator - there is one on

each of your computers. To get to it the steps each of your computers. To get to it the steps are: are: Start->Programs->Accessories->Calculator.) Start->Programs->Accessories->Calculator.)

What are the results of What are the results of (> x 3) (> x 3) ((andand (> 4 x) (> x 3)) (> 4 x) (> x 3))

(= (* x x) x) (= (* x x) x) for (a) x = 4, (b) x = 2, and (c) x = 7/2 ? for (a) x = 4, (b) x = 2, and (c) x = 7/2 ?

Page 3: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Correction from last class: eq? and equal?

• The eq? predefined function always returns false for sentences (a.k.a. strings).

• There is another predefined function equal? that works just like eq?, except that it also works on sentences/strings.

• For simplicity, we will just also use equal?

Page 4: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Conditions

Conditions are most useful in the context of a program.

• We call the functions that use them conditional functions.

• We formulate them using conditional expressions.

Page 5: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

I. Evaluating Conditional ExpressionsI. Evaluating Conditional Expressions

Page 6: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Question 1: Type into Definitions Window

;Variable Definition:(define AGE 10)

;Function Call:(cond

[(< AGE 3) ‘toddler][(and (>= AGE 3) (< AGE 13)) ‘pre-teen][(and (>= AGE 13) (< AGE 20)) ‘teen][(>= AGE 20) ‘adult])

; returns ‘pre-teenExperiment by changing AGE & re-executing

Page 7: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Syntax Rule #4: ConditionalsSyntax Rule #4: Conditionals

((cond cond [question … answer][question … answer]……[question … answer]) [question … answer])

Often, the last part is what answer do you Often, the last part is what answer do you want for everything else, in which case, the want for everything else, in which case, the last question is last question is elseelse..

((cond cond [question … answer][question … answer]……[[elseelse answer]) answer])

Page 8: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Question 2: Exercise 4.3.1

Decide which of these expressions is legal:

(cond (cond

[(< n 10) 20] [(< n 10) 20]

[(> n 20) 0] [(and (> n 20) (<= n 30))]

[else 1]) [else 1])

Page 9: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Exercise 4.3.1 solution

• The expressions are identical except for the third line so look at those.

• The second expression does not have an answer on the third line:

[(and (> n 20) (<= n 30))]

so it is an illegal expression.

Page 10: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Question 3: Why is this illegal?

(cond

[(< n 10) 20]

[ * 10 n]

[else 555])

Page 11: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Because…

There is no question on the third line.

Page 12: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

CW5: part 2 on ConditionalsCW5: part 2 on Conditionals Complete this simplified version of Exercise 4.3.3 Complete this simplified version of Exercise 4.3.3

from from How to Design ProgramsHow to Design Programs without using without using DrScheme.DrScheme.

Feel free to use a calculator - there is one on each Feel free to use a calculator - there is one on each of your computers. To get to it the steps are: of your computers. To get to it the steps are: Start->Programs->Accessories->Calculator.) Start->Programs->Accessories->Calculator.)

What is the value of:What is the value of:    (cond(cond     [(<= N 1000) (* .040 1000)]     [(<= N 1000) (* .040 1000)]     [(<= N 5000) (+ 40 (* (- N 1000) .045))]     [(<= N 5000) (+ 40 (* (- N 1000) .045))]     [else (+ 220 (* (- N 5000) .055))])     [else (+ 220 (* (- N 5000) .055))])when n is (a) 500, (b) 2800, and (c) 15000when n is (a) 500, (b) 2800, and (c) 15000

Page 13: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

II. Defining a Conditional FunctionII. Defining a Conditional Function

Page 14: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Question 4: interest-rate

A bank pays higher interest rates 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%

Page 15: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

How do we write this in Scheme?Use a conditional

(cond

[… …]

[… …]

[… …]

[… …])

Page 16: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

How do we write this in Scheme?Filling in the questions

(cond

[(<= amount 1000) …]

[(<= amount 5000) …]

[(<= amount 10000) …]

[( > amount 10000) …])

Page 17: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

How do we write this in Scheme?Filling in the answers

(cond

[(<= amount 1000) .040]

[(<= amount 5000) .045]

[(<= amount 10000) .055]

[( > amount 10000) .060])

Page 18: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Write the program

(define (interest-rate amount)

(cond

[(<= amount 1000) .040]

[(<= amount 5000) .045]

[(<= amount 10000) .055]

[( > amount 10000) .060]))

Page 19: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Strategy for conditionals

• Identify number of cases; write cond with

that many clauses

• If answers are simple, write all the answers

& then go back to fill in questions

• If questions are simple, write all the

questions & then fill in answers

Page 20: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

III. III. DesignDesigning Conditional Functionsing Conditional Functions

Page 21: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Design Recipe – Version 2Design Recipe – Version 2

Figure out precisely what you need to do.Figure out precisely what you need to do.

1. Understand the problem1. Understand the problem2. Function contract2. Function contract3. NEW STEP – Data Analysis3. NEW STEP – Data Analysis4. Write examples (in Scheme notation)4. Write examples (in Scheme notation)

Tell the computer how to do it.Tell the computer how to do it.5. Write a skeleton5. Write a skeleton6. NEW STEP – Choose a template6. NEW STEP – Choose a template

7. Fill in the function body7. Fill in the function body Check that the computer does it right. Check that the computer does it right.

8. Testing and debugging8. Testing and debugging

Page 22: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• Purpose

;To determine the interest rate given an ;amount of money in the bank.

• Contract; interest-rate: number -> number

Page 23: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• NEW STEP: Data Analysis;We take in a number and determine which ;of four intervals it is in.

;We print out a different number for each of ;the intervals.

Page 24: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• ExamplesBorderline Cases

;; (interest-rate 0) -> ??;; (interest-rate 1000) -> ??;; (interest-rate 5000) -> ??;; (interest-rate 10000) -> ??

Interval Cases;; (interest-rate 500) -> ??;; (interest-rate 3000) -> ??;; (interest-rate 7000) -> ??;; (interest-rate 12000) -> ??

Page 25: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• ExamplesBorderline Cases

;; (interest-rate 0) -> .040;; (interest-rate 1000) -> .040;; (interest-rate 5000) -> .045;; (interest-rate 10000) -> .055

Interval Cases;; (interest-rate 500) -> .040;; (interest-rate 3000) -> .045;; (interest-rate 7000) -> .055;; (interest-rate 12000) -> .060

Page 26: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• Skeleton

(define (interest-rate amount)

… amount … )

Page 27: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• NEW STEP: TemplateSo far we only have one template, for conditionals,so we will use that one.

(define (interest-rate amount) (cond

[ question answer ] … [ question answer ])

Page 28: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• NEW STEP: TemplateSince we have four cases, we will have four lines ofquestions and answers, so the template is refined asfollows:

(define (interest-rate amount) (cond

[ question answer ] [ question answer ]

[ question answer ] [ question answer ]))

Page 29: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• Fill in the Function Body --First fill in the questions.

(define (interest-rate amount) (cond

[(<= amount 1000) answer] [(<= amount 5000) answer] [(<= amount 10000) answer]

[( > amount 10000) answer]))

Page 30: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• Fill in the Function Body --Now fill in the answers.

(define (interest-rate amount)(cond

[(<= amount 1000) .040] [(<= amount 5000) .045] [(<= amount 10000) .055] [( > amount 10000) .060]))

Page 31: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

“interest-rate” again

• Testing and Debugging

--As always, type each of your examples into the interactions window.

--If you get an error message or unexpected answer, debug the program to find your mistake.

Page 32: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Question 5: Testing a Program

Suppose one of your classmates wrote these examples for interest-rate. Test the program with each of these examples:

• (interest-rate 4000) -> .045

• (interest-rate 20000) -> .060

• (interest-rate 6000) -> .040

Page 33: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Question 5: Testing a Program

• Which example did not return the expected answer?

• Is there a mistake in the program?

• What step in the design recipe did he / she make the mistake?

Page 34: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Question 5: Testing a Program

• Which example did not return the expected answer? the third one

• Is there a mistake in the program?

• What step in the design recipe did he / she make the mistake?

Page 35: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

Question 5: Testing a Program

• Which example did not return the expected answer? the third one

• Is there a mistake in the program? no

• What step in the design recipe did he / she make the mistake? writing the example

Page 36: CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160

In summary…• Functions like interest-rate need to determine

which of several conditions holds for the input.

• We call them conditional functions.

• A conditional function without numbers.

• CW6: Practice with Conditionals.

• Using conditional functions to enhance our

Animations.

Coming up…