(Redirected from Forever (block))

Forever
forever
Category Control
Type C, Cap Block
Introduced in 11Oct03 (0.x)

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, or a Stop () cap 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

The loop will wait until the next frame after each iteration, provided there are blocks running in the project that require a "yield". At 30 FPS this delay is approximately 1/30 sec. This delay can be avoided by turning on turbo mode. It can be run inside a custom block set to run without screen refresh but it is not recommended unless it is guaranteed to stop using a stop [ v] block set to stop "this script" or "all". It will cause extreme lag otherwise as Scratch doesn't get a chance to run anything else.

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.