CSC 153: Computer Science Fundamentals Grinnell College Spring, 2005
Laboratory Exercise Reading

Conditional Execution in Scheme


This reading introduces Boolean expressions and reviews how these may be used for executing statements under specified conditions.

Boolean Values and Expressions

As with many contemporary programming languages, Scheme contains the Boolean values true, written #t, and false, written #f.

The Scheme language allows Boolean values to be combined, using the operators "and", "or", and "not". When these are applied to Boolean values (using Scheme's usual prefix notation), the results follow the conventions from mathematics. Thus,

More generally, "and" and "or" may take as many arguments as desired. "and" is true if all arguments are true, and "or" is true if any of the arguments is true.

Important Note: Scheme is reasonably liberal in its evaluation of expressions, in that anything that is not explicitly false (#f) is considered true.

Scheme's Cond Statement

Scheme's cond statement uses expressions to determine what action is to be taken. This is illustrated in the following procedure:

    (define type-of-number
        (lambda (A)
            (cond ((< A 0) "the number is negative")
                  ((> A 0) "the number is positive")
                  (else    "the number is zero")
Within this cond expression, (< A 0) is first examined. If it is true, the clause "the number is negative" is evaluated and returned. [Evaluation of the cond expression is finished.] If, however, the expression (< A 0) is false, then evaluation proceeds with the next expression (> A 0). Again, if this expression is true, the following expression "the number is positive" is evaluated, and evaluation of cond is completed. But if this expression also is false, then we move to the next part of cond. else signifies that if we get this far, then the action that follows should be evaluated.

Scheme's if expression uses a conditional expression to determine which of two actions to take. This is illustrated in the following procedure:

    (define is-negative
        (lambda (A)
            (if (< A 0) 
                    "the number is negative"
                    "the number is nonnegative"

While cond and if expressions have similar capabilities for branching, note that cond and if have important syntactical differences.

Choosing Between cond and if

For most purposes, it is recommended that you use the cond expression, rather than an if expression. Of course, if may be used whenever you want to choose between just two alternatives. However, in programming, it is common to begin coding while considering just two options, only to decide that additional choices are needed later on. With a cond expression, adding such conditions is very easy. With a if expression, either you will need to rewrite the work as a cond, or you will need to nest one if within another. Either approach with ifis somewhat cumbersome.

This document is available on the World Wide Web as

created February 2, 1997
last revised January 22, 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at