Home
Learn NetLogo
What is NetLogo? Tutorial #0 Sample Model Tutorial #1 Models Tutorial #2 Commands Tutorial #3 Procedures
Documentation
NetLogo Dictionary Interface Guide Interface Tab Guide Info Tab Guide Code Tab Guide Programming Guide Transition Guide Preferences Guide Version History
Advanced Tools
Extension Manager Shapes Editor BehaviorSpace System Dynamics HubNet HubNet Authoring Logging Controlling Mathematica Link NetLogo 3D NetLogoLab Cluster Computing (HPC)
Extensions
Extensions Guide Arduino Array Bitmap CSV GIS GoGo LevelSpace Matrix Networks Palette Profiler Python Resource Rnd Sound Simple R Table Time Vid View2.5D
FAQ
Home
Learn NetLogo
What is NetLogo? Tutorial #0 Sample Model Tutorial #1 Models Tutorial #2 Commands Tutorial #3 Procedures
Documentation
NetLogo Dictionary Interface Guide Interface Tab Guide Info Tab Guide Code Tab Guide Programming Guide Transition Guide Preferences Guide Version History
Advanced Tools
Extension Manager Shapes Editor BehaviorSpace System Dynamics HubNet HubNet Authoring Logging Controlling Mathematica Link NetLogo 3D NetLogoLab Cluster Computing (HPC)
Extensions
Extensions Guide Arduino Array Bitmap CSV GIS GoGo LevelSpace Matrix Networks Palette Profiler Python Resource Rnd Sound Simple R Table Time Vid View2.5D
FAQ
primitive: ifelse
NetLogo Dictionary
  • No items found

ifelse 1.0

ifelse boolean1 commands1 elsecommands

(ifelse boolean1 commands1 boolean2 commands2 … elsecommands)

6.1

For the first boolean that reports true, runs the commands that follow.

If no boolean reports true, runs elsecommands or does nothing if elsecommands is not given. When using only one booleanyou do not need to surround the entire ifelse primitive and its blocks in parentheses.

If a boolean reports a value other than true or false a runtime error will occur.

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

The reporters may report a different value for different agents, so some agents may run different command blocks. When using more than one boolean you must surround the whole ifelse primitive and its blocks in parentheses. This functionality was added in NetLogo 6.1.

ask patches [
let choice random 4
(ifelse
choice = 0 [
set pcolor red
set plabel "r"
]
choice = 1 [
set pcolor blue
set plabel "b"
]
choice = 2 [
set pcolor green
set plabel "g"
]
; elsecommands
[
set pcolor yellow
set plabel "y"
])
]

See also if, ifelse-value.

Take me to the full NetLogo Dictionary.

NetLogo Dictionary: if

Documentation for the if primitive.

NetLogo Dictionary: ifelse-value

Documentation for the ifelse-value primitive.


NetLogo is a programmable modeling environment for simulating natural and social phenomena. It was authored by Uri Wilensky in 1999 and has been in continuous development ever since at the Center for Connected Learning and Computer-Based Modeling.

Related Links
  • NetLogo Home
  • CCL Home
  • NetLogo Web
  • NetTango Web
  • NetLogo 3D
  • BehaviorSearch
  • Contact Us

Copyright © 1999-2026 Uri Wilensky and the Center for Connected Learning and Computer-Based Modeling at Northwestern University . All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License , or (at your option) any later version.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at [email protected] .

For more information, visit the NetLogo website .