Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dotimes
loop
clojure
communitycreator

What is the dotimes statement in Clojure?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 xx amount of times.

A flowchart representation of dotimes in Clojure

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)
Using the dotimes loop

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, 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.

RELATED TAGS

dotimes
loop
clojure
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring