# Basic question on looping

**URL:** <https://discuss.ocaml.org/t/basic-question-on-looping/9736>\
**Category:** Learning\
**Created:** [April 27, 2022, 1:15pm UTC](https://discuss.ocaml.org/t/basic-question-on-looping/9736 "2022-04-27T13:15:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![devosalain](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/devosalain/32/3325_2.png) [@devosalain](https://discuss.ocaml.org/u/devosalain)\
**Post date:** [April 27, 2022, 1:15pm UTC](https://discuss.ocaml.org/t/basic-question-on-looping/9736/1 "2022-04-27T13:15:47Z")

</div>

How do i print the number 1 unto 100 without using some higher library function ?  
How do i do it in a loop ?  
[This questions refers offcourse to mutable state]

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [April 27, 2022, 2:10pm UTC](https://discuss.ocaml.org/t/basic-question-on-looping/9736/2 "2022-04-27T14:10:08Z")

</div>

```auto
for i = 1 to 100 do
  print_endline (string_of_int i)
done

```

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![devosalain](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/devosalain/32/3325_2.png) [@devosalain](https://discuss.ocaml.org/u/devosalain)\
**Post date:** [April 27, 2022, 11:02pm UTC](https://discuss.ocaml.org/t/basic-question-on-looping/9736/3 "2022-04-27T23:02:09Z")

</div>

Does i has as type “ref int” ?  
Can you print the type of i in the loop ?

---

<div class="post-metadata">

**Author:** ![nated](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nated/32/6354_2.png) [@nated](https://discuss.ocaml.org/u/nated)\
**Post date:** [April 28, 2022, 12:47am UTC](https://discuss.ocaml.org/t/basic-question-on-looping/9736/4 "2022-04-28T00:47:04Z")

</div>

`i` is just an int there. One hint that it’s not a ref is that you would have to do `!i` or `i.contents` to get the value out of a `ref int` (in `string_of_int i` in nojb’s examle). If you specifically wanted to see what it would be like with a mutable int, it might be something like:

```auto
let i = ref 0 in
while !i <= 100 do
  print_endline (string_of_int !i);
  incr i
done

```

(`incr` is a function for incrementing a mutable int, it could also be written `i := !i + 1`)

But it makes more sense to do what nojb mentioned, unless there’s some other context that requires a mutable int for some reason.

---

<div class="post-metadata">

**Author:** ![cvine](https://avatars.discourse-cdn.com/v4/letter/c/90db22/32.png) [@cvine](https://discuss.ocaml.org/u/cvine)\
**Post date:** [April 28, 2022, 8:43am UTC](https://discuss.ocaml.org/t/basic-question-on-looping/9736/5 "2022-04-28T08:43:43Z")

</div>

It is perhaps worth mentioning that ocaml implements tail call elimination, so if you are not going to use a `for` loop it would be more idiomatic in a functional language like ocaml to use recursive iteration instead of mutable state:

```auto
let () =
  let rec loop i =
    print_endline (string_of_int i) ;
    if i <= 100 then loop (i + 1) in
  loop 1

```
