Emacs Lisp: If then else (Conditional)

By Xah Lee. Date: . Last updated: .

The form for “if” expression is:

(if test body)

or

(if test true_body false_body)

(if (< 3 2)
    (progn 8)
  (progn 7))
;; 8

;; no false clause, return nil
(if (< 3 2)
    (message "yes")) ; nil

when

If you do not need a “else” part, you should use the function when instead, because it is more clear. The form is:

(when test expr1 expr2 etc)

Its meaning is the same as

(if test (progn expr1 expr2 etc) nil)

(info "(elisp) Control Structures")

Lisp Basics


Lisp Basics

Basics

Lisp Data Structure

Function

Lisp Symbol

Lisp Misc

Working with Elisp