The block documented in this article is a hidden block, meaning that while it technically still exists in the latest version of Scratch (3.0), it is not available in the palette. |
For Each () in () | |
for each [i v] in [10] { }:: #c3af00 | |
Category | Experimental (in 2.0), Control (in 3.0) |
Type | C |
The for each () in () block was a C block and an experimental block. The block had a dropdown which contained a list of all variables. When run, the selected variable was set to 1, and the block repeated the given number of times, with the variable increasing by 1 each time it looped. Like all of the other experimental blocks in Scratch 2.0, it was a hidden block, meaning it could not be accessed via the block palette.
This block is still present in Scratch 3.0 as a hidden block, and its functionality was kept intact. Additionally, the block was moved to the Control category.
The block's variable dropdown input broke in a later Scratch 2.0 alpha version,[when?] but was fixed in Scratch 3.0.
Workaround
- Main article: List of Block Workarounds
This block's functionality can still be accessed with the following workaround:
set [i v] to [0] repeat (10) change [i v] by (1) . . . end
Example Uses
This block loops a certain number of times and increments a certain variable, making it useful to loop through a List or Variable.
Some possible use cases of the block include:
- Counting up from 1 to 10
say [Let's count from 1 to 10!] for each [variable v] in [10] { say (variable) for (1) secs }:: #c3af00
- Changing each item in a list
for each [variable v] in (length of [list v]) { replace item (variable) of [list v] with [5] }:: #c3af00
- Counting the number of a certain letter in a word
ask [Enter a word.] and wait set [count v] to (0) for each [variable v] in (length of (answer)) { if <(letter (variable) of (answer)) = [a]> then change [count v] by (1) end }:: #c3af00 say (join[Your word has ](join(count)[ As!]))