How to resolve the algorithm Conditional structures step by step in the Clojure programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Conditional structures step by step in the Clojure programming language

Table of Contents

Problem Statement

List the conditional structures offered by a programming language. See Wikipedia: conditionals for descriptions. Common conditional structures include if-then-else and switch. Less common are arithmetic if, ternary operator and Hash-based conditionals. Arithmetic if allows tight control over computed gotos, which optimizers have a hard time to figure out.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Conditional structures step by step in the Clojure programming language

Source code in the clojure programming language

(if (= 1 1) :yes :no) ; returns :yes

(if (= 1 2) :yes :no) ; returns :no

(if (= 1 2) :yes) ; returns nil


(when x
  (print "hello")
  (println " world")
  5) ; when x is logical true, prints "hello world" and returns 5; otherwise does nothing, returns nil


(cond
  (= 1 2) :no) ; returns nil

(cond
  (= 1 2) :no
  (= 1 1) :yes) ; returns :yes


(cond
  (= 1 2) :no
  :else :yes) ; returns :yes


(condp < 3
  4 :a  ; cond equivalent would be (< 4 3) :a
  3 :b
  2 :c
  1 :d) ; returns :c


(condp < 3
  4 :a
  3 :b
  :no-match) ; returns :no-match


(case 2
  0 (println "0")
  1 (println "1")
  2 (println "2")) ; prints 2.


  

You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the Ring programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the Java programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Craft Basic programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Groovy programming language