if

 

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:

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.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the if primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the if primitive:
 
 
Similar primitives:
ifelse

Carries out one set of rules if a given condition is true, and another set of rules if a given condition is false.

Read more
ifelse-value

Picks a value based on a provided list of condition-value pairs.

Read more
any?

Checks if there is at least one agent in an agentset.

Read more
all?

Checks if a reporter is true for all the agents in an agentset.

Read more
 
Learn another primitive: