Skip to content

Commit

Permalink
Merge pull request #123 from moonbitlang/add-control-flow-examples
Browse files Browse the repository at this point in the history
Add continue and break examples
  • Loading branch information
bzy-debug authored Dec 18, 2023
2 parents 21fccc8 + 237f6c0 commit cac2758
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,26 @@ while x == y {

The `while` statement doesn't yield anything; it only evaluates to `()` of unit type. MoonBit also provides the `break` and `continue` statements for controlling the flow of a loop.

The `while` loop can have an optional "continue" block after the loop condition, separated by comma. It is executed *after* the body of every iteration, *before* the condition of next iteration:
```rust
var i = 0
var n = 0

while i < 10 {
i = i + 1
if (i == 3) {
continue
}

if (i == 8) {
break
}
n = n + i
}
// n = 1 + 2 + 4 + 5 + 6 + 7
println(n) // outputs 25
```

The `while` loop can have an optional "continue" block after the loop condition, separated by comma. It is executed _after_ the body of every iteration, _before_ the condition of next iteration:

```rust
var i = 0
Expand Down
21 changes: 21 additions & 0 deletions zh-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,25 @@ while x == y {
`while` 语句不返回任何值;它只求值成 `unit` 类型的 `()`
MoonBit 还提供 `break``continue` 语句来控制循环流。

```rust
var i = 0
var n = 0

while i < 10 {
i = i + 1
if (i == 3) {
continue
}

if (i == 8) {
break
}
n = n + i
}
// n = 1 + 2 + 4 + 5 + 6 + 7
println(n) // outputs 25
```

`while` 循环还可以有一个可选的 "continue" 块。"continue" 块位于循环条件和循环体之间,和循环条件用逗号分隔。它会在每次循环的循环体之后、下一次循环的循环条件之前被执行:

```rust
Expand Down Expand Up @@ -758,7 +777,9 @@ fn two[X: I]() -> X {
```

## 自动实现内建接口

Moonbit 可以自动生成一些内建接口的实现:

```
struct T {
x: Int
Expand Down

0 comments on commit cac2758

Please sign in to comment.