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

84 lines
1.8 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# For
2016-04-02 01:22:47 +02:00
Repeat code a preset number of times.
2016-03-26 00:47:20 +01:00
### @parent js/language
Repeat code a fixed number of times.
### Block Editor
![](/static/mb/events-0.png)
The Block Editor *for* loop is different than the Touch Develop *for* loop in an important way. The above for loop will iterate *five* times, with the loop variable *i* taking on values 0, 1, 2, 3, and 4. The Touch Develop for loop shown below will iterate four times:
```
for (let k = 0; k < 4; k++) {
}
```
### Touch Develop
### ~hide
```
let upper = 5
```
### ~
```
for (let k1 = 0; k1 < upper; k1++) {
// Add code to repeat here, also called the `loop body`
}
```
where
* `0` is initial value of the loop index variable `k`
* the value of `k` increases by 1 after each execution of the `loop body`
* `upper` is the number of times the loop body will repeat
In other words, the index variable (`k`) starts at 0 and increases by 1 each time the `loop body` executes, until `k = upper`.
### Example: count to 5
The following example displays numbers 1 through 5 on the LED screen:
```
for (let i = 0; i < 5; i++) {
basic.showNumber(i + 1, 100)
basic.pause(500)
}
```
### Example: draw a box
The [LED screen](/microbit/device/screen) has a fixed number of rows and columns (5x5), which is ideal for a for loop. This example uses a for loop to turn on the LEDs along the edge of the screen, making a square.
```
for (let i1 = 0; i1 < 5; i1++) {
led.plot(0, i1)
led.plot(4, i1)
led.plot(i1, 0)
led.plot(i1, 4)
basic.pause(500)
}
```
### ~hint
Want to exit a loop early? The [break](/microbit/js/break) statement exits a loop before the end value is reached.
### ~
### Lessons
[looper](/microbit/lessons/looper), [strobe light](/microbit/lessons/strobe-light)
### See also
[while](/microbit/js/while), [break](/microbit/js/break), [if](/microbit/reference/logic/if)