[previous] [up] [next]     [index]
Next: Functions that Test Conditions Up: Conditional Expressions and Functions Previous: Conditional Expressions and Functions

Booleans and Relations

external

Consider the following problem statement:

Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work, if the number is within the proper range.
The italic words highlight the new part (compared to section [cross-reference]). They imply that the program must deal with its input in one way if it is in the legitimate range, and in a different way if it is not. In short, just as people need to reason about conditions, programs must compute in a conditional manner.

Conditions are nothing new. In mathematics we talk of true and false claims, which are conditions. For example, a number may be equal to, less than, or greater than some other number. If x and y are numbers, we state these three claims about x and y with

  1. x = y: ``x is equal to y'';
  2. x < y: ``x is strictly less than y'';
  3. x > y: ``x is strictly greater than y''.
For any specific pair of (real) numbers, exactly one of these claims holds. If x=4 and y=5, the second claim is a true statement, and the others are false. If x=5 and y=4, however, the third claim is true, and the others are false. In general, a claim is true for some values of the variables and false for others.

In addition to determining whether an atomic claim holds in a given situation, it is sometimes important to determine whether combinations of claims hold. Consider the three claims above, which we can combine in several ways:

  1. x = y and x < y and x > y
  2. x = y or x < y or x > y
  3. x = y or tex2html_wrap_inline71609
The first compound claim is false because no matter what numbers we pick for x and y, two of the three claims are false. The second compound claim, however, always holds no matter what numbers we pick for x and y. Finally, the third kind of compound claim is the most important of all, because it is true in some cases and false in others. For example, it holds when x = 4, y = 4 and x = 4, y = 5, but it is false if x = 5 and y = 3.

Like mathematics, Scheme has ``words'' for expressing truth and falsity, for stating atomic claims, for combining claims into compound claims, and for expressing that a claim is true or false. The ``word'' for true is true and the ``word'' for false is false. If a claim concerns the relationship between two numbers, it can typically be expressed with a RELATIONAL OPERATION, for example, =, <, and >.

Translating the three mathematical claims from above follows our well-known pattern of writing a left parenthesis, followed by the operator, its arguments, and a right parenthesis:

  1. (= x y): ``x is equal to y'';
  2. (< x y): ``x is strictly less than y''; and
  3. (> x y): ``x is strictly greater than y''.
We will also encounter <= and >= as relational operators.

A Scheme expression that compares numbers has a result just like any other Scheme expression. The result, however, is true or false, not a number. That is, when an atomic Scheme claim about two numbers is true, it evaluates to true. For example,

  (< 4 5) 
= true 
Similarly, a false claim evaluates to false:
  (= 4 5) 
= false 

external

Expressing compound conditions in Scheme is equally natural. Suppose we want to combine (= x y) and (< y z) so that the compound claim holds if both conditions are true. In Scheme we would write

(and (= x y) (< y z))
to express this relationship. Similarly, if we want to formulate a compound claim that is true if (at least) one of two claim holds, we write
(or (= x y) (< y z))
Finally, when we write something such as
(not (= x y))
we state that we wish the negation of a claim to be true.[footnote]

Compound conditions, like atomic conditions, evaluate to true or false. Consider the following compound condition:

(and (= 5 5) (< 5 6))
It consists of two atomic claims: (= 5 5) and (< 5 6). Both evaluate to true, and therefore the evaluation of the and-expression continues as follows:
  ... 
= (and true true) 
= true 
The last step follows because, if both parts of an and-expression are true, the entire expression evaluates to true. In contrast, if one of the two claims in an and-expression evaluates to false, the and-expression evaluates to false:
  (and (= 5 5) (< 5 5))
= (and true false) 
= false 
The evaluation rules for or and not are similarly intuitive. The next few sections will explain why programming requires formulating conditions and reasoning about them.


Exercises

Exercise 4.1.1

What are the results of the following Scheme conditions?

  1. (and (> 4 3) (<= 10 100))
  2. (or (> 4 3) (= 10 100))
  3. (not (= 2 3)) Solution

Exercise 4.1.2

What are the results of

  1. (> x 3)
  2. (and (> 4 x) (> x 3))
  3. (= (* x x) x)
for (a) x = 4, (b) x = 2, and (c) x = 7/2 ? Solution


[previous] [up] [next]     [index]
Next: Functions that Test Conditions Up: Conditional Expressions and Functions Previous: Conditional Expressions and Functions

PLT