diff --git a/misc_docs/syntax/language_for.mdx b/misc_docs/syntax/language_for.mdx new file mode 100644 index 000000000..96908a4f4 --- /dev/null +++ b/misc_docs/syntax/language_for.mdx @@ -0,0 +1,45 @@ +--- +id: "for" +keywords: ["for", "loop"] +name: "for loop" +summary: "This is the `for` loop." +category: "languageconstructs" +--- + +ReScript supports `for` loops. + +For loops can iterate from a starting value up to (and including) the ending value via the `to` keyword, or in the opposite direction via the `downto` keyword. + +### Example + + + +```res +// Using `to` +for x in 1 to 3 { + Console.log(x) +} + +// Using `downto` +for y in 3 downto 1 { + Console.log(y) +} +``` + +```js +for(var x = 1; x <= 3; ++x){ + console.log(x); +} + +for(var y = 3; y >= 1; --y){ + console.log(y); +} +``` + + + + +### References + +* [For Loops](control-flow.md#for-loops) + diff --git a/misc_docs/syntax/language_while.mdx b/misc_docs/syntax/language_while.mdx new file mode 100644 index 000000000..87f9b3b4b --- /dev/null +++ b/misc_docs/syntax/language_while.mdx @@ -0,0 +1,50 @@ +--- +id: "while" +keywords: ["while", "loop"] +name: "while loop" +summary: "This is the `while` loop." +category: "languageconstructs" +--- + +ReScript supports `while` loops. While loops execute its body code block while its condition is true. + +ReScript does not have the `break` keyword, but you can easily break out of a while loop by using a mutable binding. + +### Example + + + +```res +let break = ref(false) + +while !break.contents { + if Math.random() > 0.3 { + break := true + } else { + Console.log("Still running") + } +} + +``` + +```js +let $$break = { + contents: false +}; + +while (!$$break.contents) { + if (Math.random() > 0.3) { + $$break.contents = true; + } else { + console.log("Still running"); + } +}; +``` + + + + +### References + +* [While Loops](control-flow.md#while-loops) +