pxt-calliope/docs/reference/loops/while.md

33 lines
930 B
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# While
### @parent blocks/language
2016-04-13 17:27:45 +02:00
Repeat code while a [Boolean](/reference/types/boolean) `condition` is true.
2016-03-26 00:47:20 +01:00
2016-05-19 22:56:24 +02:00
```blocks
while(true) {
}
2016-03-26 00:47:20 +01:00
```
2016-04-13 17:27:45 +02:00
The while loop has a *condition* that evaluates to a [Boolean](/reference/types/boolean) value. After the `do` keyword, add the code that you want to run while the `condition` is `true`. The while loop concludes with `end while`.
2016-03-26 00:47:20 +01:00
The condition is tested before any code runs. Which means that if the condition is false, the code inside the loop doesn't execute.
### Example: diagonal line
The following example uses a while loop to make a diagonal line on the LED screen (points `0, 0`, `1, 1`, `2, 2`, `3, 3`, `4, 4`).
2016-05-19 22:56:24 +02:00
```blocks
let index = 4;
while(index >= 0) {
led.plot(index, index);
index--;
}
```
2016-03-26 00:47:20 +01:00
### See also
2016-04-18 17:33:09 +02:00
[on button pressed](/reference/input/on-button-pressed), [for](/reference/loops/for), [if](/reference/logic/if), [forever](/reference/basic/forever)
2016-03-26 00:47:20 +01:00