ClojureDocs

Nav

Namespaces

Evaluates test.
3 Examples
(defn is-small? [number]
  (if (< number 100) "yes" "no"))

user=> (is-small? 50)
"yes"

user=> (is-small? 500)
"no"
;; Be aware that the only two values considered "logical false" in Clojure
;; are nil and false, where Clojure's "false" is the Java value Boolean/FALSE
;; under the hood.  Everything else is "logical true".  Particularly surprising
;; may be that the Java Object with class Boolean and value (Boolean. false) is
;; considered logical true.

;; This notion of logical true and logical false holds for at least the following
;; conditional statements in Clojure: if, cond, when, if-let, when-let.
;; It also applies to functions like filter, remove, and others that use
;; these conditional statements in their implementation.

;; nil and false are logical false
user=> (if nil "logical true" "logical false")
"logical false"
user=> (if false "logical true" "logical false")
"logical false"
;; Boolean/FALSE is how Clojure's "false" is represented internally.
user=> (if Boolean/FALSE "logical true" "logical false")
"logical false"

;; Everything else that is the value of the condition, including numbers (even 0),
;; characters, strings (even the empty string), vectors, maps, _and_ a freshly
;; constructed Boolean class object (Boolean. false), is logical true.

user=> (if 0 "logical true" "logical false")
"logical true"
;; A vector containing nil is not the same as nil.
user=> (if [nil] "logical true" "logical false")
"logical true"
user=> (if (first [nil]) "logical true" "logical false")
"logical false"

;; Bad idea even in Java.  See below for more details.
user=> (if (Boolean. false) "logical true" "logical false")
"logical true"

;; Java documentation itself warns:
;; Note: It is rarely appropriate to use this constructor. Unless a new instance
;; is required, the static factory valueOf(boolean) is generally a better choice.
;; It is likely to yield significantly better space and time performance.

;; (boolean x) converts a value to a primitive boolean.  It converts nil, false,
;; and (Boolean. false) to primitive false.
user=> (if (boolean (Boolean. false)) "logical true" "logical false")
"logical false"

;; (Boolean/valueOf <val>) is similar:
user=> (if (Boolean/valueOf (Boolean. false)) "logical true" "logical false")
"logical false"
;; if vs. when

;; An if without an else branch...
user=> (if true "then branch")
"then branch"
user=> (if false "then branch")
nil

;; ...is the same as when.
user=> (when true "then branch")
"then branch"
user=> (when false "then branch")
nil

;; Use of when instead of if is recommended in cases where you do not need the else branch.
See Also

Takes a set of test/expr pairs. It evaluates each test one at a time. If a test returns logical t...

Added by Havvy

Evaluates test. If logical true, evaluates body in an implicit do.

bindings => binding-form test If test is true, evaluates then with binding-form bound to the valu...

Added by jafingerhut

Evaluates test. If logical false, evaluates and returns then expr, otherwise else expr, if suppli...

Added by jhulten

bindings => binding-form test When test is true, evaluates body with binding-form bound to the va...

Added by jafingerhut
1 Note
    By , created 9.6 years ago
    user=> (doc if)
    -------------------------
    if
      (if test then else?)
    Special Form
      Evaluates test. If not the singular values nil or false,
      evaluates and yields then, otherwise, evaluates and yields else. If
      else is not supplied it defaults to nil.
    
      Please see http://clojure.org/special_forms#if
    

    http://clojure.org/special_forms#if