NetLogo Help: ifelse-value

### ifelse-value2.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 boolean you 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