The for
loop is used to execute a code block over a given amount of time. This loop is very useful in Ocaml or other programming languages.
for var = initial_value to final_value doexpressiondone
Let's look at the code below:
for i = 1 to 10 doprint_int(i)done;;
In the above code, we use the for
loop to print all the numbers from 1-10
.
for
loop and define the range for the loop i = 1 to 10
. The loop stops once i
reaches 10
. After the do
keyword, we put the code block
we want to execute while the loop is running.done
keyword to stop the for
loop.