### foreach

#### foreach list command-task
(foreach list1 ... command-task)

With a single list, runs the task for each item of list.

    foreach [1.1 2.2 2.6] show
    => 1.1
    => 2.2
    => 2.6
    foreach [1.1 2.2 2.6] [ show (word ? " -> " round ?) ]
    => 1.1 -> 1
    => 2.2 -> 2
    => 2.6 -> 3

With multiple lists, runs commands for each group of items from each list. So, they are run once for the first items, once for the second items, and so on. All the lists must be the same length.

Some examples make this clearer:

    (foreach [1 2 3] [2 4 6]
       [ show word "the sum is: " (?1 + ?2) ])
    => "the sum is: 3"
    => "the sum is: 6"
    => "the sum is: 9"
    (foreach list (turtle 1) (turtle 2) [3 4]
      [ ask ?1 [ fd ?2 ] ])
    ;; turtle 1 moves forward 3 patches
    ;; turtle 2 moves forward 4 patches

See also map, ?.

Take me to the full NetLogo Dictionary