# Class 10: Conditionals

Back to Procedure Definitions. On to Conditionals Lab.

Held: Tuesday, 4 February 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:

Assignments

Notes:

• I may have to leave class a few minutes early to pick up my son.
• Are there questions on yesterday's lab?

Overview:

• A problem: Turning numbers into letters (in English and Scheme).
• Making life easier: 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.
• 90-100: A, 80-89: B, 70-79: C, 60-69: D, 0-59: F.
• 4: A, 3: B, 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 the standard 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 (<= 90 grade) 'A 'F)
```
• We can nest these conditions.
```(if (<= 90 garde)
'A
'B
...)))
```
• We can take advantage of knowledge that previous tests failed.
```(if (<= grade 90)
'A
'B
...)))
```

## 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
...)
```

## A Challenge

• Can you write code to convert numeric to letter that uses neither `if` nor `cond`?
• Hint: You can do it with just the ideas you learned before today.

## 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, 16 January 2003 [Samuel A. Rebelsky]

• First version, created mostly automatically from previous course.

Tuesday, 4 February 2003 [Samuel A. Rebelsky]

• Updated body slightly.

Back to Procedure Definitions. 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:29:40 2003.
The source to the document was last modified on Tue Feb 4 11:04:48 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Outlines/outline.10.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu