Iterative Execution

NetLogo provides seven commands for iteratively (repeatedly) executing a block of commands. In order of usefulness they are:

while [CONDITION] [COMMANDS]

repeat NUMBER [COMMANDS]

loop [COMMANDS]

every SECONDS [COMMANDS]

foreach LIST [COMMANDS]

ask AGENTS [COMMANDS]

ask-concurrent AGENTS [COMMANDS]

Example 1: The while command

Use the while command when a block must be executed an unpredictable number of times:

Assume turtles have random amounts of energy while patches have tiny random amounts of penergy:

turtles-own [energy]
patches-own [penergy]

A grazing turtle moves and eats until it runs out of energy:

to graze
   pd
   while [energy > 0]
   [
      rt random 360
      fd random 5
      set energy energy – 5 + [penergy] of patch-here
   ]
end

Note: Use the of operator to ask a turtle or patch to report the value of some attribute:

[ATTRIBUTE] of AGENT

Example 2: The repeat command

Use the repeat command when you can predict the number of times a block should be executed.

In this procedure a turtle decrements the health of his victim 10 times:

to attack [victim]
  repeat 10
  [
      ; strike victim:
      ask victim [set energy energy - random 10]
  ]
end

Example 3: The loop command

Use the loop command when a block must be executed forever. These are called perpetual loops and are particularly useful for command interpreters:

to echo
  loop
  [
    let command user-input "enter your name or q to quit"
    if command = "q" [print "bye" stop]
    print word "Hello, " command
  ]
end

Note: Executing the user-input reporter causes the following dialog box to appear:

Pressing the OK button causes the value entered ("Chuck") to be reported.

Example 4: The every command

The every command executes its block perpetually, but with delays:

to gradually-move-turtles
   every 1.5 [ask turtles [move]]
end

This is equivalent to:

to gradually-move-turtles
   loop
   [
      wait 1.5
      ask turtles [move]
   ] 
end

Example 5: The foreach command

The foreach command takes two inputs, a list and a parameterized block. A parameterized block contains the '?' symbol. This symbol is replaced by every item in the list, and the resulting block is executed.

For example:

to print-squares [num-list]
   foreach num-list [print ? * ?]
end

Here's a sample call:

observer> print-squares [1 2 3 4 5]
1
4
9
16
25

To appreciate the power of this command read more about lists.

Example 6: The ask and ask-concurrent commands

The ask commands are similar to the foreach command. Instead of repeatedly executing a block of commands each time with a different member from some list, the ask commands  iterate execution of a block of commands each time with a different agent from a set of agents.

The difference between ask and ask-concurrent can best be understood by studying the following example:

globals [counter]

to inc-counter
  set counter counter + 1
  print (word "turtle # " who " incrementing counter")
  print word "count = " counter
end


to demo-ask
  set counter 0
  crt 3
  ask turtles [inc-counter inc-counter]
  print "+++++"
  ask-concurrent turtles [inc-counter inc-counter]
end

  Here's the output produced:

observer> demo-ask
turtle # 2 incrementing counter
count = 1
turtle # 2 incrementing counter
count = 2
turtle # 1 incrementing counter
count = 3
turtle # 1 incrementing counter
count = 4
turtle # 0 incrementing counter
count = 5
turtle # 0 incrementing counter
count = 6
+++++
turtle # 0 incrementing counter
count = 9
turtle # 2 incrementing counter
count = 10
turtle # 1 incrementing counter
count = 11
turtle # 0 incrementing counter
count = 12
turtle # 2 incrementing counter
count = 12
turtle # 1 incrementing counter
count = 12