diff --git a/README.md b/README.md index 5cc9c81a..df8a09b6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/zh-docs/README.md b/zh-docs/README.md index 77e58d45..3eff8f24 100644 --- a/zh-docs/README.md +++ b/zh-docs/README.md @@ -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 @@ -758,7 +777,9 @@ fn two[X: I]() -> X { ``` ## 自动实现内建接口 + Moonbit 可以自动生成一些内建接口的实现: + ``` struct T { x: Int