Like all languages, Scheme provides conditionals. The basic form is the if:
(iftest-expressionthen-branchelse-branch)
If test-expression evaluates to true (ie, any value
other than #f), the ``then'' branch is evaluated.
If not, the ``else'' branch is evaluated. The
``else'' branch is optional.
Assuming pressure of tube is less than
60, this conditional will attach floor-pump to
tube and depress it 5 times. (attach
and depress are some suitable procedures.)
The cond form is convenient for expressing nested
if-expressions, where each ``else'' branch but the last
introduces a new if. Thus, the form
(if (char<?c#\c) -1
(if (char=?c#\c) 01))
can be rewritten using cond as:
(cond ((char<?c#\c) -1)
((char=?c#\c) 0)
(else1))
The cond is thus a multi-branch conditional. Each
clause has a test and an associated action. The first
test that succeeds triggers its associated action. The
final else clause is chosen if no other test
succeeded.
Scheme provides special forms for boolean conjunction
(``and'') and disjunction (``or''). (We have already
seen (sec 2.1.1) Scheme's boolean negation
not, which is a
procedure.)
The special form
and returns a true value if all its subforms are true. The
actual value returned is the value of the final
subform. If any of the subforms are false, and
returns #f.
(and12) =>2
(and#f1) =>#f
The special form or returns the value of its
first true subform. If all the subforms are false,
or returns #f.
(or12) =>1
(or#f1) =>1
Both and and or evaluate their subforms
left-to-right. As soon as the result can be
determined, and and or will ignore the
remaining subforms.