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

75 lines
1.8 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# While
2016-04-02 01:22:47 +02:00
Repeat code in a loop while a condition is true.
2016-03-26 00:47:20 +01:00
### @parent js/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
### ~hide
```
let condition = false
```
### ~
### Block Editor
![](/static/mb/string-0.png)
### Touch Develop
```
while (condition) {
// This code runs if `condition` is `true`
}
```
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
2016-04-13 17:27:45 +02: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. Use the [break statement](/js/break) to exit a while loop before it's complete.
2016-03-26 00:47:20 +01:00
### 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
```
let index = 4
while (index >= 0) {
led.plot(index, index)
// // subtract 1 from `index` each time through loop
index = index - 1
}
```
### Example: count until A button pressed
The following example shows numbers on the screen(0, 1, 2, 3...), until the "A" button is pressed.
```
let x = 0
while (true) {
basic.showNumber(x, 100)
let pressed = input.buttonIsPressed("A")
if (pressed) {
break
} else {
x = x + 1
}
basic.pause(1000)
}
```
### Lessons
2016-04-13 17:27:45 +02:00
[rotation animation](/lessons/rotation-animation), [digi yoyo](/lessons/digi-yoyo)
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[on button pressed](/reference/input/on-button-pressed), [for](/reference/loops/for), [if](/reference/logic/if), [break](/js/break), [forever](/reference/basic/forever), [in background](/reference/control/in-background)
2016-03-26 00:47:20 +01:00