if
“If it is a good weather, I will go to a park; otherwise, I’ll go to a cafe.” This is called “if-branching” in the programming world. Since the if-branching uses simple conditionals, it is frequently used to divide into two states: true or false.
In Clojure, if
is a special form. The syntax is:
(if test then else?)
or (if test then)
First, the Truthiness
of the test is checked.
If the test turns out to be true, the then part is run; otherwise, the else? part is run (if specified).
The else? part is optional.
As the if
syntax shows, Clojure doesn’t have else if.
If you need to be able to write else if, you can use cond
, case
and some more conditionals.
In addition to if
, some languages have an unless conditional which runs only if the test is false.
Clojure implements this with the if-not
macro. The syntax is:
(if-not test then else?)
or (if-not test then)
Advice to coaches
You can show the other way to implement unless: (if (not (= ….)))
This would be an example of how Clojure has many ways to do the same thing.
In addition, Clojure has a unique way of using the if
conditional with the let
binding.
It is if-let
macro, which is useful when we want to use the result of test.
The syntax is:
(if-let bindings then)
or (if-let bindings then else & oldform)
ClojureDocs
Special Froms, if
http://clojure.org/special_forms#Special Forms–(if test then else?)
Introduction to Clojure
http://clojure-doc.org/articles/tutorials/introduction.html#control-structures
Clojure for the Brave and True, Control Flow, if
GetClojure