44 lines
1.1 KiB
Markdown
Raw Normal View History

2016-03-25 16:47:20 -07:00
# While
2016-04-01 16:22:47 -07:00
Repeat code in a loop while a condition is true.
2016-03-25 16:47:20 -07:00
### @parent blocks/language
2016-04-13 08:27:45 -07:00
Repeat code while a [Boolean](/reference/types/boolean) `condition` is true.
2016-03-25 16:47:20 -07:00
### ~hide
```
let condition = false
```
### ~
### Block Editor
![](/static/mb/string-0.png)
2016-04-13 08:27:45 -07: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-25 16:47:20 -07: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`).
// index is set to 4
![](/static/mb/blocks/var-10.png)
// subtract 1 from `index` each time through loop
### Lessons
2016-04-13 08:27:45 -07:00
[rotation animation](/lessons/rotation-animation)
2016-03-25 16:47:20 -07:00
### See also
2016-04-18 08:33:09 -07:00
[on button pressed](/reference/input/on-button-pressed), [for](/reference/loops/for), [if](/reference/logic/if), [forever](/reference/basic/forever)
2016-03-25 16:47:20 -07:00