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