CSC 160 Computer Programming for Non-Majors Chapter 4: Conditional Expressions and Functions 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

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
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
Section 4.2: Functions that Test Conditions (continued)
CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Images: Another Form of Symbolic Data 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 #1: What is Programming? 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
CSC 160 Computer Programming for Non-Majors Introduction Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Syntax Rules Prof. Adam M. Wittenstein
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
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
CSC 160 Computer Programming for Non-Majors Section 1.3: Animating the UFOs Flight Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Introduction Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Section 1.2: Drawing a UFO Prof. Adam M. Wittenstein
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
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
CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 3: Conditional Expressions.
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
XOR and XNOR Logic Gates. XOR Function Output Y is TRUE if input A OR input B are TRUE Exclusively, else it is FALSE. Logic Symbol  Description  Truth.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
Logic Disjunction A disjunction is a compound statement formed by combining two simple sentences using the word “OR”. A disjunction is true when at.
Compound Data. Last Lecture Booleans Only two elements Symbols Not to be confused with variables Strings Notions of equality on values.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Functions Alice.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Functional Programming Language 1 Scheme Language: part 2.
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 4: Understand and implement Decisions.
A: A: double “4” A: “34” 4.
 Can facial expressions tell if someone is lying or not?
Section 3.1 Functions. OBJECTIVE 1 A relation is a correspondence between two sets. If x and y are two elements in these sets and if a relation exists.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Logic You will learn three common logical operations that will be applied in much of the course (spreadsheets, databases, web searches and both programming.
Compare/Contrast Class Quiz
Basic Logical Operations (Fascinating)
Review Operation Bingo
If Statement Functions in Excel
3.4 Computer systems Boolean logic Lesson 2.
Lesson Quizzes Standard Lesson Quiz
4105 CHALLENGING REVIEW QUIZ
True or false: 76% is a parameter.
Section 3.7 Switching Circuits
February , 2009 CSE 113 B.
Exponents Chapter 4 Section 4.2.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Compound Conditionals
Millennium High School Agenda Calendar
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Chapter 4: Conditional Expressions and Functions Prof. Adam M. Wittenstein

Dealing with Conditions To deal with conditions, we need to have a way of saying a condition is true or false. We need a new class of values, which are called booleans or truth values.

Section 4.1: Booleans and Relations

Another type of data: Boolean (= (+ 3 4) 7) returns true (> (+ 3 4) 7) returns false = : number number -> boolean > : number number -> boolean Same for =, <=

Built-in operations on Booleans (and true false) returns false (or true false) returns true (not true) returns false not : boolean -> boolean and, or: boolean boolean … -> boolean

Example 1: True or false On paper, decide what results you would expect from the following Scheme conditions: On paper, decide what results you would expect from the following Scheme conditions: 1) (and (> 3 4) (>= )) 2) (or (> 3 4) (= )) 3) (not (= 4 (+ 1 3)))

Is the parameter of the right type? To determine if an object (number, boolean, symbol, string, image, etc.) is of a given type, DrScheme has predefined functions: boolean? : object -> boolean (tells whether the object is a boolean) number? : object -> boolean (tells whether the object is a boolean) There are similar functions called string?, symbol?, and image?. eq? : object object -> boolean (tells whether two objects are the same)

Section 4.2: Functions that Test Conditions

Our first Boolean-valued function ; under-21? : number -> boolean “Examples of under-21?:” (under-21? 17) “should be” true (under-21? 48) “should be” false (under-21? 21) “should be” false ; Idiom: functions returning boolean end with ?

Our first Boolean-valued function ; under-21? number -> boolean (define (under-21? age) … age … ) “Examples of under-21?:” (under-21? 17) “should be” true (under-21? 48) “should be” false (under-21? 21) “should be” false

Our first Boolean-valued function ; under-21? number -> boolean (define (under-21? age) (< age 21) ) “Examples of under-21?:” (under-21? 17) “should be” true (under-21? 48) “should be” false (under-21? 21) “should be” false

In Summary… Booleans are another atomic (simple) data type. They are used to test if something is true or false. By convention, a Scheme function that returns a boolean has a ? at the end of its name. Other than that, booleans work the same way as all the other data types we have worked with this semester. Next time… Writing more boolean-valued functions Discuss HW7 Review for Quiz