I think recursion is the simplest way to go…
defmodule Looper do
def say_hello(times_left) do
case times_left do
0 ->
:ok
x ->
IO.puts("hello")
say_hello(x - 1)
end
end
end
Looper.say_hello(10)
You could use if instead of case since there are only two patterns.
Alternatively:
defmodule Looper do
def say_hello(times_left) when times_left > 0 do
IO.puts("hello")
say_hello(times_left - 1)
end
def say_hello(times_left) when times_left == 0 do
:ok
end
end
Looper.say_hello(10)


















