# Class 05: Conditionals

Back to Procedures. On to Conditionals Lab.

Held: Monday, 27 January 2003

Summary: Today we begin to learn how to tell Scheme to make decisions. The procedures used to make decisions are called conditionals. Today's class will be a large group design effort rather than an individual or small group design effort.

Related Pages:

Due

Assignments:

Notes:

• Now that you've played with my site design for awhile, do you have any suggestions for improving it?
• I'm trying an alternative to the whiteboard today. I'd like to hear your feedback tomorrow.
• I've extended my office hours. I am now also available MTWF 9:00-9:50.
• I forgot to assign the reading on Boolean values. You can spend some of today's class looking at it (and should read over it tonight).

Overview:

• A problem: Turning numbers into letters (in English and Scheme).
• Making life easier: Conditionals.
• What kinds of tests can you use in conditionals?

## A Problem

• Suppose that for fairness's sake, I'd like a program that converts numeric grades to letter grades.
• We'll look at two related grading schemes.
• 94-100: A, 90-93: B, 87-89: B+, 84-86: B, ...
• 10:A+ 9:A, 8:A-, 7:B+, 6:B, 5:B-, 4:C+ 3:C 2:C- 1:D 0:F
• How might you explain to someone how to turn a number into a grade?
• In either schema, can you write a procedure that turns a number into a grade, using only the Scheme you already know?

## First Solution: Use if

• As you may have noted from your narrative, we are often tempted to describe the procedure in terms of the word if. For example, if the grade is at least 94, then give the student an A, otherwise, if the grade is between 90 and 93, ...
• In Scheme, you can write such expressions as
(if condition
success-expression
failure-expression)

• Note that if does not behave according to Marie's evaluation pattern (in which all the arguments to a procedure are computed before the procedure is applied).
• In particular, only one of the success-expression and failure-expression is computed.
• Given the Scheme that you know right now, the only effect is on efficiency.
• Later, you'll see that this decision affects other things, too.
• For example
(if (>= grade 94) 'A 'F)

• We can nest these conditions.
'A
'Aminus
...)))

## Another Solution

• As you may have noticed, the code is somewhat hard to read (and awfully indented). Another alternative is to use Scheme's cond, which more closely resembles a table.
• Here's what a cond expression looks like
(cond
((condition1) exp1)
((condition2) exp2)
((condition3) exp3)
...
(else default-exp))

• In this case, the conditions are evaluted one-by-one until one holds true. The value of the corresponding expression is the then returned.
• For our problem, this format seems particularly appropriate.
(cond
...)

## Boolean Expressions

• To permit you to write useful tests and conditions for conditionals, Scheme provides a wide variety of mechanisms for building those tests.
• Tests should return true (#t) or false (#f).
• Scheme treats anything that's not false as true.
• We call the tests Boolean expressions in honor of the logician George Boole.
• We call procedures that return true or false predicates.
• The simplest tests: Type Predicates.
• These procedures (whose names invariably end with question marks) determine whether a value falls within a particular type.
• They include: number?, list?, and symbol?.
• There are many numeric comparators, including < (strictly less than), <= (less than or equal to), = (equal to), >= (greater than or equal to), and > (strictly greater than).
• There are also a variety of equality comparators, including =, eq?, eqv?, and equal?.
• = is used for numbers.
• eq? determines if two values share the same memory location.
• eqv? determines if two primitive values are equal.
• equal? also looks within compound types (such as lists).
• Scheme also lets you combine Boolean expressions with and, or, and not.
• You will have an opportunity to experiment with all of these in tomorrow's lab.

## History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Monday, 27 January 2003 [Samuel A. Rebelsky]

• Body copied from previous course.
• Added section on Boolean expressions.

Back to Procedures. On to Conditionals Lab.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:20:18 2003.
The source to the document was last modified on Mon Jan 27 09:19:06 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.05.html.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu