rhai/doc/src/language/loop.md

27 lines
567 B
Markdown
Raw Normal View History

2020-06-20 06:06:17 +02:00
Infinite `loop`
===============
{{#include ../links.md}}
2020-06-21 18:03:45 +02:00
Infinite loops follow C syntax.
Like C, `continue` can be used to skip to the next iteration, by-passing all following statements;
`break` can be used to break out of the loop unconditionally.
2020-06-20 06:06:17 +02:00
```rust
let x = 10;
loop {
x = x - 1;
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
if x > 5 { continue; } // skip to the next iteration
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
print(x);
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
if x == 0 { break; } // break out of loop
}
```
2020-06-21 18:03:45 +02:00
Beware: a `loop` statement without a `break` statement inside its loop block is infinite -
there is no way for the loop to stop iterating.