banner-nll

The Logic of Conditional Commands

To effectively program in NetLogo - or in any computer language, it is important to have a strong grasp of conditional logic. You will often want a command to be executed if certain conditions are true.

For example, if you are driving a car your behavior at a traffic light is determined by the “state” of the light.  If the light is green, you would keep driving, if the light is yellow, you should come to a stop if it is possible, if the light is red, you should stop imediately.

Each time you approach an traffic light, you are in effect processing an IF-THEN statement.

Here is how the traffic light could be implemented in NetLogo:

    to go
    drive-car  ;a procedure to cause the car to move forward
    If light-color = red [stop-car]
    If light-color = yellow [stop-car]

    end

But this statement could be written more efficiently if it combined the two conditions for stopping.

Look at this code as an alternative:

    to go
    drive-car
    if (light-color = red) OR (light-color = yellow) [stop-car]
    end

More about If-then statements

When using an IF statement, NetLogo can only report a boolean (true-false) value.  What this means is that when NetLogo evaluates the statement (is the light red), the answer is either true or false.  If it is red, it is true, and NetLogo then processes the “then” part of the command. If it is green, then NetLogo reads the statement as being false, and it ignores the “then” statement.

IFELSE Statements

An IF statement gives you the choice of carrying out some instructions or doing nothing at all.  Sometimes we want to carry out either of two sets of commands.  An IFELSE statement lets us accomplish this.[Footnote]
 
An ifelse statement evaluates a condition (is the traffic light red?) And then executes one command if it is true (stop) or a second command if it is false (keep driving). 
 
Or for another example, consider someone who has an afternoon off from work and wants to do one of two things: go to a movie, or go play golf.  You make your choice based on the weather. If it is warm and sunny outside, go play golf. If it is wet and rainy, go to the movie.  Here is how this would be implemented in NetLogo:
 
    Ifelse weather = rain [go-to-movie] [get-a-tee-time]
 

The syntax for an ifelse statement is the same as an IF statement, except there is a second set of brackets at the end.

Here is a “real” example:

    ask patches[ ifelse pxcor > 0[ set pcolor blue ][ set pcolor red ] ]
    ;; the left half of the screen turns red and
    ;; the right half turns blue

Continue this study guide


With Support from:

The NetLogo Learning Lab is part of modelingcomplexity.org, the home of the Mesa State College Center for Agent-Based Modeling.

This website is copyright by Mesa State College, 2004. All rights are reserved.

Some materials are adapted from the NetLogo User manual, and are copyright Wilensky, U. (1999). NetLogo.  Center for Connected Learning and Computer-Based Modeling. Northwestern University, Evanston, IL.