Conditional Execution

NetLogo has two types of conditional execution commands, one-way and two-way:

if CONDITION [COMMANDS]

ifelse CONDITION [THEN-COMMANDS] [ELSE-COMMANDS]

Example 1: One-way conditional execution

In this model turtles perpetually move and reproduce. Both activities consume energy. When a turtle's energy is gone, it dies.

The program begins with the declaration of energy and model initialization:

turtles-own [energy]

to init-model
   ca
   crt 10
   [
      set energy random 100
      setxy random-xcor random-ycor
   ]
end

Note: The create-turtle command (crt) takes an optional block as a second input:

crt N [INIT-COMMANDS]?

Every turtle created by the command will execute this block. Typically, this is where newly created turtles initialize their attributes.

The update-model procedure will be executed repeatedly by a run button. An if command is used to stop the procedure when there are no more turtles. This will halt the simulation.

to update-model
   if count turtles = 0 [stop]
   ask turtles [update-turtle]
end

to update-turtle
   check-energy
   move
   reproduce
end
  
to move
   rt random 360
   fd random 5
   dec-energy 5
end

Moving decrement's a turtle's energy. We use a special procedure to do this so that a turtle's energy won't be negative:

to dec-energy [amt]
   if amt < 0
   [
      print "input to dec-energy must be positive"
      stop
   ]
   set energy energy - amt
   if energy < 0 [ set energy 0 ]
end

Notice that if commands are used twice. Once to validate the input, printing an error message and stopping the procedure if the input isn't valid, and again to set the energy to 0 if it is negative.

The check-energy procedure uses an if command to cause the current turtle to die if its energy is zero. Otherwise, the turtle prints its energy to the Command Center window:

to check-energy
   if energy = 0 [die]
   print word "energy = " energy
end

Note the use of the word reporter to concatenate a string with a number.

Finally, the reproduce procedure uses an if command to reproduce only if the turtle has sufficient energy:

to reproduce
   if energy > 50
   [
      hatch 1 [ set energy random 100 ]
      dec-energy 10
   ]
end

Note: Like crt and sprout, the hatch command takes an optional initialization block as a second input:

hatch N [INIT-COMMANDS]

Every turtle created by the command will execute this block. Typically, this is where newly hatched turtles initialize their attributes.

Example 2: Two-way conditional execution

Suppose energetic turtles (health > 50) hatched two babies, while weaker ones (health <= 50) hatched only one. To implement this we could modify the reproduce procedure above using a two-way conditional command:

to reproduce
  ifelse energy > 50
  [
    hatch 2 [ set energy random 100 ]
    dec-energy 10
  ]
  [
    hatch 1 [ set energy random 100 ]
    dec-energy 5
  ]
end

Example 3: Multi-way conditional execution

Multi-way conditionals (three or more blocks) can be implemented by nesting two-way conditionals:

ifelse CONDITION1
[
   STATEMENTS1
]
[
   ifelse CONDITION2
   [
      STATEMENTS2
   ]
   [
      STATEMENTS3
   ]
]

For example, suppose very energetic turtles (energy > 70) hatch three babies when they reproduce, moderately energetic turtles hatched two babies, and otherwise only one baby is hatched. We could implement this by modifying the reproduce procedure with a 3-way conditional expression:

to reproduce
  ifelse energy > 70
  [
    hatch 3 [ set energy random 100 ]
    dec-energy 15
  ]
  [
    ifelse energy > 30
    [
      hatch 2 [ set energy random 100 ]
      dec-energy 10
    ]
    [
      hatch 2 [ set energy random 100 ]
      dec-energy 10
    ]
  ]
end

Note: Why don't we need to check that energy is < 70?

Example 4: Conditional expressions

The ifelse-value expression takes three inputs:

ifelse-value CONDITION [THEN-EXPRESSION] [ELSE-EXPRESSION]

If the condition is true, then the value of this expression is the value of the then-expression, otherwise it's the value of the else-expression.

Here's an example:

to init-patch
   set pcolor ifelse-value (pxcor > 0) [blue] [red]
end

Note: Commands update variables while expressions report values.