pxt-calliope/docs/reference/basic/forever.md

84 lines
2.3 KiB
Markdown
Raw Normal View History

# forever
2016-03-26 00:47:20 +01:00
Keep running part of a program
[in the background](/reference/control/in-background).
2016-03-26 00:47:20 +01:00
```sig
basic.forever(() => {
})
```
You can have part of a program continuously by placing it in an **forever** loop. The **forever** loop will _yield_ to the other code in your program though, allowing that code to have time to run when needs to.
### ~ reminder
#### Event-based loops
Both the **forever** loop and the **every** loop are _event-based_ loops where the code inside is run as part of a function. These are different from the [for](/blocks/loops/for) and [while](/blocks/loops/while) loops. Those are loops are part of the programming language and can have [break](/blocks/loops/break) and [continue](/blocks/loops/continue) statements in them.
You can NOT use **break** or **continue** in either a **forever** loop or an **every** loop.
### ~
## Examples
### Example: compass
2016-03-26 00:47:20 +01:00
The following example constantly checks the
[compass heading](/reference/input/compass-heading)
and updates the screen with the direction.
2016-03-26 00:47:20 +01:00
```blocks
let degrees = 0
2016-03-26 00:47:20 +01:00
basic.forever(() => {
degrees = input.compassHeading()
if (degrees < 45) {
basic.showString("N")
} else if (degrees < 135) {
basic.showString("E")
} else if (degrees < 225) {
basic.showString("S")
} else if (degrees < 315) {
basic.showString("W")
} else {
basic.showString("N")
2016-03-26 00:47:20 +01:00
}
})
```
### Example: counter
2016-03-26 00:47:20 +01:00
The following example keeps showing the [number](/types/number) stored in a global variable.
When you press button `A`, the number gets bigger.
You can use a program like this to count things with your @boardname@.
2016-03-26 00:47:20 +01:00
```blocks
let num = 0
basic.forever(() => {
basic.showNumber(num)
2016-03-26 00:47:20 +01:00
})
input.onButtonEvent(Button.A, input.buttonEventClick(), () => {
2016-03-26 00:47:20 +01:00
num = num + 1
})
```
## Competing for the LED screen
2016-03-26 00:47:20 +01:00
If different parts of a program are each trying
to show something on the LED screen at the same time,
you may get unexpected results.
2016-11-02 01:44:37 +01:00
Try this on your @boardname@:
2016-03-26 00:47:20 +01:00
```blocks
basic.forever(() => {
basic.showNumber(6789)
2016-03-26 00:47:20 +01:00
})
input.onButtonEvent(Button.A, input.buttonEventClick(), () => {
basic.showNumber(2)
2016-03-26 00:47:20 +01:00
})
```
## See also
2016-03-26 00:47:20 +01:00
[while](/blocks/loops/while), [in background](/reference/control/in-background), [every](/reference/loops/every-interval)
2016-03-26 00:47:20 +01:00