“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.
if is a special form. The syntax is:
(if test then else?) or
(if test then)
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.
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
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)
Special Froms, if
Introduction to Clojure
Clojure for the Brave and True, Control Flow, if