What is the dotimes statement in Clojure?
Overview
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.
Syntax
(dotimes (variable value)
statement)
Parameters
The dotimes statement takes a numerical value that represents the number of times the loop will execute.
Code example
(ns clojure.examples.hello(:gen-class));; Dotimes loop(defn doTimesFunc [](dotimes [x 9](println x)))(doTimesFunc)
Explanation
-
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,
dotimesrequires 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.