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 dropdown contained a list of all variables. When run, the selected variable is set to 1, and the block repeated the given number of times, with the variable increasing by 1 each time it looped. Like most experimental counter blocks, 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 is fully functional. 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.


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)
    . . .

Example Uses

This block loops a certain amount 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 amount of a certain letter in a word

ask [Enter a word.] and wait
set [amount v] to (0)
for each [variable v] in (length of (answer)) {
  if <(letter (variable) of (answer)) = [a]> then
    change [amount v] by (1)
}:: #c3af00
say (join[Your word has ](join(amount)[ As!]))

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