Forever

Repeat code in the background forever.

Block Editor

Touch Develop

basic → forever do
add code here
end

Example: compass

The following example constantly checks the compass heading and updates the screen with the direction.
basic → forever do
var heading := input → compass heading
if heading < 45 then
basic → show string("N", 100)
else if heading < 135 then
basic → show string("E", 100)
else if heading < 225 then
basic → show string("S", 100)
else
basic → show string("W", 100)
end if
end

Example: counter

The following example continually shows the current value of a global variable:
 num := 0
basic → forever do
basic → show number( num, 150)
end
input → on button pressed(A) do
 num :=  num + 1
end

Contention for the LED display

If you have multiple processes that each show something on the LED screen, you may get unexpected results. Try, for example:
basic → forever do
basic → show number(6789, 150)
end
input → on button pressed(A) do
basic → show number(2, 150)
end

Lessons

See also