OCaml for loops

Loops are used to iterate and execute statements for each iteration.

Each Iteration starts with an initial value, incremented by 1 until the end_value matched

for variable = start_value to end_value do
     expression_statements
done

The expression_statements always returns a unit value in OCAML. It changes values and does not return value.

Here is an example

for k = 0 to 5 do
      Printf.printf "%d\n" k
done;;

Ocaml While Loops

while loop to repeat execute statements until conditioal_expression is true

while conditional_expression do 
     expression 
done

Here is an example

let number = 1 in
while number<5 do
   let number = number+1 in
   ()
done
Printf.printf "Output %d\n" number