- "Repeat" redirects here. For the block that repeats until a certain condition is true, see Repeat Until () (block).
Repeat () | |
repeat (10) | |
Category | Control |
Type | C |
The Repeat () block is a Control block and a C block. Blocks held inside this block will loop a given amount of times, before allowing the script to continue.
If a decimal is put in, the number is rounded up. Furthermore, when a negative number or a non-number is input, the loop does not run, and if "Infinity" is input, then the block runs forever.[1]
This block has a slight delay, so for optimum speed of execution, Single Frame block stacks should be used.
Example Uses
As this block loops pieces of code, it is widely used for saving space and time. Some common uses are:
- Repeating code — using the Repeat () block (and the Repeat Until () block) can save a lot of scripting.
repeat (10) change [variable v] by (1) next costume if <touching (Wall v)?> then erase all broadcast (Reload v) else stamp end
- Animation — rather than coding each costume change and delay individually, the Repeat () block (with the Next Costume block) can be used. This can reduce unnecessary scripting.
repeat (8) wait (0.05) secs next costume
- Continuous checks — a sprite can continuously move and sense, for example.
repeat (20) change y by (3) if <touching (edge v)?> then change [color v] effect by (75)
- Putting one repeat () block inside another is called "nesting". The total repetitions will be the two repeat () block inputs' product. In this case, the nested loops draw 10 times 10, or 100 dots.
set pen size to (2) go to x:(-100) y:(100) repeat (10) repeat (10) pen down pen up change x by (20) end set x to (-100) change y by (-20) end
Repetitions
The loop will repeat the number of times provided in its input slot. If the input is not a whole number, it will be rounded to the nearest one. If the input is less than or equal to zero, or not a number, it will not run any scripts inside of it. If the input is infinity, it will run forever.
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. If one do not wish to have this delay, they must turn on turbo mode or put it inside of a custom block set to run without screen refresh .
Workaround
- Main article: List of Block Workarounds
This block can be replicated with the following code:
set [counter v] to (0) set [number of times v] to (. . .::grey) // this is the number of times to repeat if <not<(number of times)<(0)>> then repeat until <(counter) = ([ceiling v] of (number of times) . . . change [counter v] by (1) end end