CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Scheme: Functions Chapter 3 of HTDP Ms. Knudtzon September 12.
Topic 17 assertions and program logic
ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences (continued) Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #8: Animations I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5: Defining Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #11: Conditionals II Prof. Adam M. Wittenstein
Section 2.4: Errors. Common errors ● Mismatched parentheses ● Omitting space after operator or between numbers ● Putting operator between operands.
Chapter 2: Algorithm Discovery and Design
Chapter 5: Control Structures II (Repetition)
Section 4.4: Designing Conditional Functions. REVIEW: Design Recipe – V. 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function.
CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3c: Working with Pictures (continued) Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Draft Chapter: The UFO Example Prof. Adam M. Wittenstein
Section 2.5: Designing Programs. REVIEW: Design Recipe  Figure out precisely what you need to do.  Tell the computer how to do it.  Check that the.
Section 4.3: Conditions and Conditional Functions.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
CSC 160 Computer Programming for Non-Majors Section 1.2: Drawing a UFO Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 4: Conditional Expressions and Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5b: Designing Programs Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #2: What are Functions? Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5a: Defining Functions on Words and Sentences Prof. Adam M. Wittenstein
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
CSC 160 Computer Programming for Non-Majors Lecture #3b: Working with Pictures Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5 (continued): More on Writing Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Functional Programming Language 1 Scheme Language: part 2.
Decisions, Decisions, Decisions Conditional Statements In Java.
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
COMP Loop Statements Yi Hong May 21, 2015.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
CS314 – Section 5 Recitation 10
1-1 Logic and Syntax A computer program is a solution to a problem.
Programming Languages Dan Grossman 2013
Conditions and Ifs BIS1523 – Lecture 8.
Programming Right from the Start with Visual Basic .NET 1/e
Rules of evaluation The value of a number is itself.
Exercise Solution First questions What's output What's input
Let's Play "What's the Question"
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein

CW5: part 1 on Booleans Complete Exercise from How to Design Programs without using DrScheme. Complete Exercise from How to Design Programs without using DrScheme. Feel free to use a calculator - there is one on each of your computers. To get to it the steps are: Feel free to use a calculator - there is one on each of your computers. To get to it the steps are:Start->Programs->Accessories->Calculator.) What are the results of What are the results of (> x 3) (and (> 4 x) (> x 3)) (= (* x x) x) for (a) x = 4, (b) x = 2, and (c) x = 7/2 ?

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?

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.

I. Evaluating Conditional Expressions

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-teen Experiment by changing AGE & re-executing

Syntax Rule #4: Conditionals ( cond [question … answer] … [question … answer]) Often, the last part is what answer do you want for everything else, in which case, the last question is else. ( cond [question … answer] … [else answer])

Question 2: Exercise Decide which of these expressions is legal: (cond(cond [(< n 10) 20] [(> n 20) 0] [(and (> n 20) (<= n 30))] [else 1])

Exercise 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.

Question 3: Why is this illegal? (cond [(< n 10) 20] [ * 10 n] [else 555])

Because… There is no question on the third line.

CW5: part 2 on Conditionals Complete this simplified version of Exercise from How to Design Programs without using DrScheme. Complete this simplified version of Exercise from How to Design Programs without using DrScheme. Feel free to use a calculator - there is one on each of your computers. To get to it the steps are: Feel free to use a calculator - there is one on each of your computers. To get to it the steps are:Start->Programs->Accessories->Calculator.) What is the value of: (cond [(<= N 1000) (* )] [(<= N 5000) (+ 40 (* (- N 1000).045))] [else (+ 220 (* (- N 5000).055))]) when n is (a) 500, (b) 2800, and (c) What is the value of: (cond [(<= N 1000) (* )] [(<= N 5000) (+ 40 (* (- N 1000).045))] [else (+ 220 (* (- N 5000).055))]) when n is (a) 500, (b) 2800, and (c) 15000

II. Defining a Conditional Function

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%

How do we write this in Scheme? Use a conditional (cond [… …] [… …])

How do we write this in Scheme? Filling in the questions (cond [(<= amount 1000) …] [(<= amount 5000) …] [(<= amount 10000) …] [( > amount 10000) …])

How do we write this in Scheme? Filling in the answers (cond [(<= amount 1000).040] [(<= amount 5000).045] [(<= amount 10000).055] [( > amount 10000).060])

Write the program (define (interest-rate amount) (cond [(<= amount 1000).040] [(<= amount 5000).045] [(<= amount 10000).055] [( > amount 10000).060]))

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

III. Designing Conditional Functions

Design Recipe – Version 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function contract 3. NEW STEP – Data Analysis 4. Write examples (in Scheme notation)  Tell the computer how to do it. 5. Write a skeleton 6. NEW STEP – Choose a template 7. Fill in the function body  Check that the computer does it right. 8. Testing and debugging

“interest-rate” again Purpose ;To determine the interest rate given an ;amount of money in the bank. Contract ; interest-rate: number -> number

“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.

“interest-rate” again Examples Borderline 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) -> ??

“interest-rate” again Examples Borderline 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

“interest-rate” again Skeleton (define (interest-rate amount) … amount … )

“interest-rate” again NEW STEP: Template So far we only have one template, for conditionals, so we will use that one. (define (interest-rate amount) (cond [ question answer ] … [ question answer ])

“interest-rate” again NEW STEP: Template Since we have four cases, we will have four lines of questions and answers, so the template is refined as follows: (define (interest-rate amount) (cond [ question answer ] [ question answer ]))

“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]))

“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]))

“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.

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

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?

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?

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

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…