**Contents**
** Next:**
Cond
**Up:**
Conditional Control
** Previous:**
Conditional Control

## If

An if statement has the form:

(if <test> <then> <else> )

The test, then, and else expressions can be any evaluable Lisp expressions -- e.g., symbols or lists. If the evaluation of the test expression returns anything other than nil, e.g. T, 3, FOO, (A S D F), the interpreter evaluates the then expression and returns its value, otherwise it returns the result of evaluating the else expression.
We can use if to define a function to return the absolute difference between two numbers, by making use of the predicate **> ** (greater than). Here it is:

(defun absdiff (x y)
(if (> x y)
(- x y)
(- y x)))

If x is greater than y, then the test, i.e. (**> ** x y), returns T, so the then clause is evaluated, in this case (- x y), which gives the positive difference. If x is less than or equal to y, then the expression (- y x) gets evaluated, which will return 0 or a positive difference.

*© Colin Allen & Maneesh Dhagat *

March 2007