Download presentation
Presentation is loading. Please wait.
1
CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/
2
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).
3
I. Booleans and Relations
4
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 =, <=
5
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
6
Example 1: True or false On paper, decide what results you would expect from the following Scheme conditions: 1) (and (> 3 4) (>= 100 10)) 2) (or (> 3 4) (= 100 10)) 3) (not (= 4 (+ 1 3)))
7
Example 1 Solution On paper, decide what results you would expect from the following Scheme conditions: 1) (and (> 3 4) (>= 100 10)) F and T = F 2) (or (> 3 4) (= 100 10)) F or T = T 3) (not (= 4 (+ 1 3))) not T = F
8
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)
9
II. Functions that Test Conditions
10
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 ?
11
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
12
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
13
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
14
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
15
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
16
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
17
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.