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

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

Ocaml While Loops

while loop to repeat execute statements until conditioal_expression is true

while conditional_expression do
     // code statements

conditional_expression evaluates to bool value, If true, executes code statements. Here is an example

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

In this example, variable number declared with a value 1.

while loop executes until number is less than 5