Clojure - Nested If Statement


Advertisements

Sometimes there is a requirement to have multiple ‘if’ statement embedded inside of each other, as is possible in other programming languages. In Clojure, this is made possible with the help of using the logical ‘and’ when evaluating multiple expressions.

Syntax

Following is the general form of this statement.

if(and condition1 condition2) statement #1 statement #2

Example

Following is an example of how multiple conditions can be implemented.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example [] (
   if ( and (= 2 2) (= 3 3))
   (println "Values are equal")
   (println "Values are not equal")))
(Example)

Output

The above code produces the following output.

Values are equal
clojure_decision_making.htm
Advertisements