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-value
NetLogo Dictionary
  • No items found

ifelse-value 2.0

ifelse-value boolean1 reporter1 elsereporter

(ifelse-value boolean1 reporter1 boolean2 reporter2 … elsereporter)

6.1

For the first boolean that reports true, runs the reporter that follows and reports that result. When using only one booleanyou do not need to surround the entire ifelse-value primitive and its blocks in parentheses.

If all booleans report false, the result is the value of elsereporter. You may leave out the elsereporter, but if all booleans report false then a runtime error will occur.

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

This can be used when a conditional is needed in the context of a reporter, where commands (such as ifelse) are not allowed.

ask patches [
set pcolor ifelse-value (pxcor > 0) [blue] [red]
]
;; the left half of the world turns red and
;; the right half turns blue
show n-values 10 [ n -> ifelse-value (n < 5) [0] [1] ]
=> [0 0 0 0 0 1 1 1 1 1]
show reduce [ [a b] -> ifelse-value (a > b) [a] [b] ]
[1 3 2 5 3 8 3 2 1]
=> 8

When using more than one boolean you must surround the whole ifelse-value primitive and its blocks in parentheses. This functionality was added in NetLogo 6.1.

ask patches [
let choice random 4
set pcolor (ifelse-value
choice = 0 [ red ]
choice = 1 [ blue ]
choice = 2 [ green ]
[ yellow ])
]

A runtime error can occur if there is no elsereporter.

ask patches [
let x = 2
set pcolor (ifelse-value
x = 0 [ red ]
x = 1 [ blue ]
; no final else reporter is given, and x is 2 so there will be a runtime error
)

See also if, ifelse.

Take me to the full NetLogo Dictionary.

NetLogo Dictionary: ifelse

Documentation for the ifelse primitive.

NetLogo Dictionary: import-drawing

Documentation for the import-drawing 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 .