Conditionals
Conditional control structures choose among alternatives. Emacs Lisp has five conditional forms: if
, which is much the same as in other languages; when
and unless
, which are variants of if
; cond
, which is a generalized case statement; and pcase
, which is a generalization of cond
(see Pattern-Matching Conditional).
- Special Form: if condition then-form else-forms…
-
if
chooses between the then-form and the else-forms based on the value of condition. If the evaluated condition is non-nil
, then-form is evaluated and the result returned. Otherwise, the else-forms are evaluated in textual order, and the value of the last one is returned. (The else part ofif
is an example of an implicitprogn
. See Sequencing.)If condition has the value
nil
, and no else-forms are given,if
returnsnil
.if
is a special form because the branch that is not selected is never evaluated—it is ignored. Thus, in this example,true
is not printed becauseprint
is never called:(if nil (print 'true) 'very-false) ⇒ very-false
- Macro: when condition then-forms…
-
This is a variant of
if
where there are no else-forms, and possibly several then-forms. In particular,(when condition a b c)
is entirely equivalent to
(if condition (progn a b c) nil)
- Macro: unless condition forms…
-
This is a variant of
if
where there is no then-form:(unless condition a b c)
is entirely equivalent to
(if condition nil a b c)
- Special Form: cond clause…
-
cond
chooses among an arbitrary number of alternatives. Each clause in thecond
must be a list. The CAR of this list is the condition; the remaining elements, if any, the body-forms. Thus, a clause looks like this:(condition body-forms…)
cond
tries the clauses in textual order, by evaluating the condition of each clause. If the value of condition is non-nil
, the clause succeeds; thencond
evaluates its body-forms, and returns the value of the last of body-forms. Any remaining clauses are ignored.If the value of condition is
nil
, the clause fails, so thecond
moves on to the following clause, trying its condition.A clause may also look like this:
(condition)
Then, if condition is non-
nil
when tested, thecond
form returns the value of condition.If every condition evaluates to
nil
, so that every clause fails,cond
returnsnil
.The following example has four clauses, which test for the cases where the value of
x
is a number, string, buffer and symbol, respectively:(cond ((numberp x) x) ((stringp x) x) ((bufferp x) (setq temporary-hack x) ; multiple body-forms (buffer-name x)) ; in one clause ((symbolp x) (symbol-value x)))
Often we want to execute the last clause whenever none of the previous clauses was successful. To do this, we use
t
as the condition of the last clause, like this:(t body-forms)
. The formt
evaluates tot
, which is nevernil
, so this clause never fails, provided thecond
gets to it at all. For example:(setq a 5) (cond ((eq a 'hack) 'foo) (t "default")) ⇒ "default"
This
cond
expression returnsfoo
if the value ofa
ishack
, and returns the string"default"
otherwise.
Any conditional construct can be expressed with cond
or with if
. Therefore, the choice between them is a matter of style. For example:
(if a b c) ≡ (cond (a b) (t c))
Copyright © 1990-1996, 1998-2021 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Conditionals.html