ifelse1.0

ifelse reporter1 [ commands1 ] [ elsecommands ] (ifelse reporter1 [ commands1 ] reporter2 [ commands2 ] ... [ elsecommands ])

The reporters must report boolean (true or false) values.

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

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

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 reporter you must surround the whole ifelse primitive and its blocks in parentheses.

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