if
is a primitive that allows us to define conditional agent behaviors. It has the following structure: if condition [ command(s) ]
. If the given condition is true, NetLogo will run the provided code within the brackets. If the given condition is false, NetLogo will not do anything.
ask cars [
if my-speed < 60 [
accelerate
]
]
Things to keep in mind when using if
:
and
and or
primitives.not
primitive to make true statements false, and vice-versa.ifelse
primitive. In the model example below, we have some patches that represent mousetraps and a few mice moving around randomly. If a mouse steps on a mousetrap, it sets its trapped?
variable to true
. The mice who are not trapped?
continue moving around, while the ones who are trapped remain stationary.
Once you mastered the if
primitive, don't stop there. Check out the resources below to improve your NetLogo skills.
if
primitive:ifelse
Carries out one set of rules if a given condition is true, and another set of rules if a given condition is false.
ifelse-value
Picks a value based on a provided list of condition-value pairs.