Forever
forever
Category Control
Type C, Cap Block

The Forever block is a Control block, a C block, and a Cap Block. Blocks held inside this block will be in a loop — just like the Repeat () block and the Repeat Until () block, except that the loop never ends (unless the stop sign is clicked, the Stop All block is activated, or the stop script block is activated within the loop). Due to this infinite loop, the block has no bump at the bottom; having a bump would be pointless, as the blocks below it would never be activated.

This block has a slight delay, so for optimal speed of execution, single frame block stacks should be used.

An example of the behavior of a forever loop

Example Uses

The block is one of the most commonly used blocks in Scratch because there are a lot of cases when an infinite loop is needed. Some common uses are:

  • Keeping a sprite at another's location
forever
go to (sprite v)
  • A music loop
forever
play sound (battle theme v) until done
forever
repeat (12)
turn left (3) degrees
end
repeat (12)
turn right (3) degrees

Workaround

Main article: List of Block Workarounds

This block can be replicated in three ways: With a boolean statement that will never be true, through recursion, or through reporters that return "Infinity".

With the boolean method, the Repeat Until () block is used, along with the boolean statement. There are many that will never be true, including:

<[1] = [-1]>
<[a] = [b]>
<(timer) = [-1]>
<not <not <>>>
<<> or <>>

A full workaround:

repeat until <[1] = [-1]>
. . .

One method that does not involve false boolean statements is tail recursion. Tail recursion can be created by a script broadcasting the broadcast needed to start it, thus repeatedly running the script:

when I receive [recursion v]
. . .
broadcast (recursion v)

This has some benefits to making projects. For example, the script is helpful since it can be used to create fractals.

This also works:

repeat ([10^ v] of (309)::operators)
. . .
end

This requires an operator that reports "Infinity", like:

([10^ v] of (309)::operators)
([e^ v] of (1000):: operators)
(join [infinity] [])
((1) / (0))
([tan v] of (90))

All of these will work in the last workaround.

Finally, one could do this as a workaround:

define foreverloop
. . .
foreverloop:: custom

Delays

After each iteration of the loop, a 1/30 of a second, or one frame, delay is added before the next iteration continues. This allows for animations to be run more smoothly. However, this delay can be bypassed by turning on turbo mode, but it is not recommended to put it inside of a custom block set to run without screen refresh unless it will be exited with a stop ( v) block set to stop 'this script' or 'all'.

Note Warning: Putting a loop that never ends inside of a custom block set to run without screen refresh will cause the project to lag tremendously.

Warnings

  • Too many Forever loops running can cause the project to lag or slow down.
  • Projects should have an escape or an alternative so they do not get stuck in a forever loop.

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.