if“Flow control” is the programming term for deciding how to react to a given circumstance. We make decisions like this all the time. If it’s a nice day out, then we should visit the park; otherwise we should stay inside and play board games. If your car’s tank is empty, then you should visit a gas station; otherwise you should continue to your destination.
Software is also full of these decisions. If the user’s input is valid, then we should save her data; otherwise we show an error message. The common pattern here is that you test some condition and react differently based on whether the condition is true or false.
ifIn Clojure, the most basic tool we have for this process is the
ifoperator. Here’s how you might code the data validation scenario.
If the
angleis less than 360, then returnangle; otherwise, calculate modulo by 360 and return it.
Reference: Conditional if
(if (< angle 360)
angle
(mod angle 360))
if operator(if conditional-expression
expression-to-evaluate-when-true
expression-to-evaluate-when-false)
if examples(if (> 3 1)
"3 is greater than 1"
"3 is not greater than 1")
;=> "3 is greater than 1"
(if (> 1 3)
"1 is greater than 3"
"1 is not greater than 3")
;=> "1 is not greater than 3"
When testing the truth of an expression, Clojure considers the values
nilandfalseto be false and everything else to be true. Here are some examples:
Reference: Truthiness
(if "anything other than nil or false is considered true"
"A string is considered true"
"A string is not considered true")
;=> "A string is considered true"
(if nil
"nil is considered true"
"nil is not considered true")
;=> "nil is not considered true"
(if (get {:a 1} :b)
"expressions which evaluate to nil are considered true"
"expressions which evaluate to nil are not considered true")
;=> "expressions which evaluate to nil are not considered true"
real-angle that takes an angle as an argument.;; if example
(if (< angle 360)
angle
(mod angle 360))
;; usage of real-angle function
(real-angle 180)
;=> 180
(real-angle 1000)
;=> 280
and, or, and not
ifstatements are not limited to testing only one thing. You can test multiple conditions using boolean logic. Boolean logic refers to combining and changing the results of predicates usingand,or, andnot.
If you’ve never seen this concept in programming before, remember that it follows the common sense way you look at things normally. Is this and that true? Only if both are true. Is this or that true? Yes, if either – or both! – are. Is this not true? Yes, if it’s false.
and,or, andnotwork like other functions (they aren’t exactly functions, but work like them), so they are in prefix notation, like we’ve seen with arithmetic.
| x | y | (and x y) |
(or x y) |
(not x) |
(not y) |
|---|---|---|---|---|---|
| false | false | false | false | true | true |
| true | false | false | true | false | true |
| true | true | true | true | false | false |
| false | true | false | true | true | false |
and, or, and not combination
and,or, andnotcan be combined. This can be hard to read. Here’s an example:
(defn leap-year?
"Every four years, except years divisible by 100, but yes for years divisible by 400."
[year]
(and (zero? (mod year 4))
(or (zero? (mod year 400))
(not (zero? (mod year 100))))))
Return to the first slide, or go to the curriculum outline.