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…
-
ifchooses 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 ofifis an example of an implicitprogn. See Sequencing.)If condition has the value
nil, and no else-forms are given,ifreturnsnil.ifis a special form because the branch that is not selected is never evaluated—it is ignored. Thus, in this example,trueis not printed becauseprintis never called:(if nil (print 'true) 'very-false) ⇒ very-false
- Macro: when condition then-forms…
-
This is a variant of
ifwhere 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
ifwhere there is no then-form:(unless condition a b c)
is entirely equivalent to
(if condition nil a b c)
- Special Form: cond clause…
-
condchooses among an arbitrary number of alternatives. Each clause in thecondmust 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…)
condtries the clauses in textual order, by evaluating the condition of each clause. If the value of condition is non-nil, the clause succeeds; thencondevaluates 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 thecondmoves on to the following clause, trying its condition.A clause may also look like this:
(condition)
Then, if condition is non-
nilwhen tested, thecondform returns the value of condition.If every condition evaluates to
nil, so that every clause fails,condreturnsnil.The following example has four clauses, which test for the cases where the value of
xis 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
tas the condition of the last clause, like this:(t body-forms). The formtevaluates tot, which is nevernil, so this clause never fails, provided thecondgets to it at all. For example:(setq a 5) (cond ((eq a 'hack) 'foo) (t "default")) ⇒ "default"This
condexpression returnsfooif the value ofaishack, 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