CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans 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

Warm Up Example 1 Check whether the ordered pair is a solution of 2x – 3y > -2 a.(0,0) 2(0)-3(0)> -2 0> -2 True b.(0,1) 2(0)-3(1)> -2 -3> -2 False.
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences (continued) Prof. Adam M. Wittenstein
CPSC 322, Lecture 20Slide 1 Propositional Definite Clause Logic: Syntax, Semantics and Bottom-up Proofs Computer Science cpsc322, Lecture 20 (Textbook.
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
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
Section 6.1: Structures (continued). Example 1: in-left-side? ; Purpose: To determine whether a point is in the leftmost 150 pixels of the screen. ; Contract.
CSC 160 Computer Programming for Non-Majors Draft Chapter: The UFO Example Prof. Adam M. Wittenstein
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
CS3: Introduction to Symbolic Programming Spring 2006Nate Titterton Lecture 2: Introduction, and Conditionals.
Section 4.3: Conditions and Conditional Functions.
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
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
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
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
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSD 340 (Blum)1 Ifs. CSD 340 (Blum)2 Page that asks user for background color and changes fore color from black if user selects black as background color.
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.
Slide 1 Propositional Definite Clause Logic: Syntax, Semantics and Bottom-up Proofs Jim Little UBC CS 322 – CSP October 20, 2014.
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.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Introduction to Computer Programming
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:
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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?
Propositional Logic Rather than jumping right into FOL, we begin with propositional logic A logic involves: §Language (with a syntax) §Semantics §Proof.
Agenda Warmup Finish 2.4 Assignments
Review Operation Bingo
If Statement Functions in Excel
3-1 Inequalities and Their Graphs
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
February , 2009 CSE 113 B.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Compound Conditionals
General Computer Science for Engineers CISC 106 Lecture 03
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Chapter 2 Sets Active Learning Lecture Slides
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans 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 data type, booleans (a.k.a. truth values).

I. Booleans and Relations

Another type of data: Boolean (= (+ 3 4) 7) returns true (> (+ 3 4) 7) returns false (>= (+ 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: 1) (and (> 3 4) (>= )) 2) (or (> 3 4) (= )) 3) (not (= 4 (+ 1 3)))

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

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 number) There are similar functions called word?, sentence?, and image?. eq? : object object -> boolean (tells whether two objects are the same)

II. Functions that Test Conditions

Example 2: “under-21?” ; Purpose: To determine if someone is under 21. ; Contract: 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 ?

Example 2: “under-21?” ; Purpose: To determine if someone is under 21. ; Contract: 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

Example 2: “under-21?” ; Purpose: To determine if someone is under 21. ; Contract: 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

Example 3: “18-to-25?” ;Purpose ;To determine if someone is between 18 and 25. ;Contract ;18-to-25? : number -> boolean “Examples of 18-to-25?:” (18-to-25? 17) “should be” false (18-to-25? 48) “should be” false (18-to-25? 21) “should be” true (18-to-25? 18) “should be” true (18-to-25? 25) “should be” true

Example 3: “18-to-25?” ; 18-to-25? : number -> boolean (define (18-to-25? age) … age …)) “Examples of 18-to-25?:” (18-to-25? 17) “should be” false (18-to-25? 48) “should be” false (18-to-25? 21) “should be” true (18-to-25? 18) “should be” true (18-to-25? 25) “should be” true

Example 3: “18-to-25?” ; 18-to-25? : number -> boolean (define (18-to-25? age) … (>= age 18) … (<= age 25) …) “Examples of 18-to-25?:” (18-to-25? 17) “should be” false (18-to-25? 48) “should be” false (18-to-25? 21) “should be” true (18-to-25? 18) “should be” true (18-to-25? 25) “should be” true

Example 3: “18-to-25?” ; 18-to-25? : number -> boolean (define (18-to-25? age) (and (>= age 18) (<= age 25))) “Examples of 18-to-25?:” (18-to-25? 17) “should be” false (18-to-25? 48) “should be” false (18-to-25? 21) “should be” true (18-to-25? 18) “should be” true (18-to-25? 25) “should be” true

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… Syntax Rule #4: Conditionals