Skip to content

Commit 927a5de

Browse files
committed
add for and while loops to the syntax widget
1 parent cdf968b commit 927a5de

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

misc_docs/syntax/language_for.mdx

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: "for"
3+
keywords: ["for", "loop"]
4+
name: "for loop"
5+
summary: "This is the `for` loop."
6+
category: "languageconstructs"
7+
---
8+
9+
ReScript supports `for` loops.
10+
11+
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.
12+
13+
### Example
14+
15+
<CodeTab labels={["ReScript", "JS Output"]}>
16+
17+
```res
18+
// Using `to`
19+
for x in 1 to 3 {
20+
Console.log(x)
21+
}
22+
23+
// Using `downto`
24+
for y in 3 downto 1 {
25+
Console.log(y)
26+
}
27+
```
28+
29+
```js
30+
for(var x = 1; x <= 3; ++x){
31+
console.log(x);
32+
}
33+
34+
for(var y = 3; y >= 1; --y){
35+
console.log(y);
36+
}
37+
```
38+
39+
</CodeTab>
40+
41+
42+
### References
43+
44+
* [For Loops](control-flow.md#for-loops)
45+

misc_docs/syntax/language_while.mdx

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
id: "while"
3+
keywords: ["while", "loop"]
4+
name: "while loop"
5+
summary: "This is the `while` loop."
6+
category: "languageconstructs"
7+
---
8+
9+
ReScript supports `while` loops. While loops execute its body code block while its condition is true.
10+
11+
ReScript does not have the `break` keyword, but you can easily break out of a while loop by using a mutable binding.
12+
13+
### Example
14+
15+
<CodeTab labels={["ReScript", "JS Output"]}>
16+
17+
```res
18+
let break = ref(false)
19+
20+
while !break.contents {
21+
if Math.random() > 0.3 {
22+
break := true
23+
} else {
24+
Console.log("Still running")
25+
}
26+
}
27+
28+
```
29+
30+
```js
31+
let $$break = {
32+
contents: false
33+
};
34+
35+
while (!$$break.contents) {
36+
if (Math.random() > 0.3) {
37+
$$break.contents = true;
38+
} else {
39+
console.log("Still running");
40+
}
41+
};
42+
```
43+
44+
</CodeTab>
45+
46+
47+
### References
48+
49+
* [While Loops](control-flow.md#while-loops)
50+

0 commit comments

Comments
 (0)