Skip to content

Commit

Permalink
add for control structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-zh committed Mar 20, 2024
1 parent 98c4489 commit 6b814fe
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,51 @@ fn init {
}
```

### For loop

For loop executes a block of code repeatedly. The loop variables are immutable, and scoped to the loop body. The `break` statement can be used to exit the loop and yield a value. The `continue` statement can be used to skip the rest of the body, start the next iteration, and update loop variables.

```rust
fn sum(xs: Array[Int]) -> Int {
let len = xs.length()
for i = 0, sum = 0 {
if i < len {
continue i + 1, sum + xs[i]
} else {
break sum
}
}
}

test "sum" {
@assertion.assert_eq(sum([1, 2, 3]), 6)?
}
```

The for loop can also be equipped with a condition expression and an increment statement. The condition is evaluated before each iteration, and the increment statement is executed after each iteration. The loop body is executed as long as the condition is true.

```rust
fn print_n(n: Int) -> Unit {
for i = 0; i < n; i = i + 1 {
print(i)
}
}
```

The `sum` function can be rewritten as follows. Note to use `else` to return the value of the loop when it terminates. If the loop does not yield a value, the `else` branch can be omitted.

```rust
fn sum(xs: Array[Int]) -> Int {
let len = xs.length()
for i = 0, sum = 0
i < len
i = i + 1, sum = sum + xs[i] {
} else {
sum
}
}
```

### While loop

In MoonBit, `while` loop can be used to execute a block of code repeatedly as long as a condition is true. The condition is evaluated before executing the block of code. The `while` loop is defined using the `while` keyword, followed by a condition and the loop body. The loop body is a sequence of statements. The loop body is executed as long as the condition is true.
Expand Down
47 changes: 46 additions & 1 deletion zh-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,52 @@ fn init {
}
```

### 循环
### for 循环

for循环可以执行一段代码多次。循环变量是不可变的,并且仅可以在循环体内部被访问。可以使用break语句退出循环并返回一个值。continue语句可以用来跳过循环体的剩余部分,开始下一次迭代,并更新循环变量。

```rust
fn sum(xs: Array[Int]) -> Int {
let len = xs.length()
for i = 0, sum = 0 {
if i < len {
continue i + 1, sum + xs[i]
} else {
break sum
}
}
}

test "sum" {
@assertion.assert_eq(sum([1, 2, 3]), 6)?
}
```

for循环还可以配备条件表达式和增量语句。循环条件在每次迭代之前进行计算,增量语句在每次迭代之后执行。只要循环条件为真,就执行循环体。

```rust
fn print_n(n: Int) -> Unit {
for i = 0; i < n; i = i + 1 {
print(i)
}
}
```

`sum`函数可以如下重写。注意使用else来返回循环终止时的值。如果循环没有产生值,可以省略else分支。

```rust
fn sum(xs: Array[Int]) -> Int {
let len = xs.length()
for i = 0, sum = 0
i < len
i = i + 1, sum = sum + xs[i] {
} else {
sum
}
}
```

### while 循环

MoonBit 的 `while` 循环可以用如下语法定义:

Expand Down

0 comments on commit 6b814fe

Please sign in to comment.