A loop executes a code block continuously until a condition is satisfied. The dotimes
statement is a Clojure loop that executes code in its code block amount of times.
(dotimes (variable value)
statement)
The dotimes
statement takes a numerical value that represents the number of times the loop will execute.
(ns clojure.examples.hello(:gen-class));; Dotimes loop(defn doTimesFunc [](dotimes [x 9](println x)))(doTimesFunc)
Line 4: We define the function where we implement the loop using dotimes
.
Line 5: We carry out our loop with dotimes
.
Note: Unlike other kinds of loops where we satisfy a condition,
dotimes
requires us to pass a variable and the number of times we want the loop to run.
Line 6: We print the value of the variable x
.
Line 7: We call the (doTimesFunc)
function.