Quick StartTryGuideAPICommunityBlogGitHub
Search

Imperative LoopsSuggest an edit

For Loops

For loops iterate from a starting value up to (and including) the ending value.

for myBinding in (startValue) to (endValue) {
  /* use myBinding here */
};

The parenthesis around startValue and endValue may be omitted if they are unnecessary.

let xStart = 1;
let xEnd = 3;
/* prints: 1 2 3 */
for x in xStart to xEnd {
  print_int x;
  print_string " ";
};

You can make the for loop count in the opposite direction by using downto.

for myBinding in (startValue) downto (endValue) {
  statements
};
let xStart = 3;
let xEnd = 1;
/* prints: 3 2 1 */
for x in xStart downto xEnd {
  print_int x;
  print_string " ";
};

While Loops

While loops execute a code block while some condition is true. The form of a while loop includes a single expression, the condition to test.

while (testCondition) {
  statements;
};

Tips & Tricks

There's no loop-breaking break keyword (nor early return from functions, for that matter) in Reason. In general, prefer map/filter/reduce over imperative loops. However, we can break out of a while loop easily through using a mutable binding.

Random.self_init ();
let break = ref false;
while (not !break) {
  if (Random.int 10 === 3) {
    break := true
  } else {
    print_endline "hello"
  }
};