E W
E W

Reputation: 61

Multiple or conditions within if command

I am trying to direct turtles towards a cone of patches if total chemical level in the cone is greater than regular chemical on either side of the turtle. However, my code involves multiple or conditions, which brings up the error that if expected 2 inputs, a TRUE/FALSE and a command block. Also, is there a way to use ifelse with multiple or conditions (e.g. ifelse chem-scent a > total-in-cone or chem-scent-b > total-in-cone etc)?

 [if chemical-scent-at-angle 0 > total-chemical-scent-in-cone 20 45 10 or chemical-scent-at-angle 60 > total-chemical-scent-in-cone 20 45 10 or chemical-scent-at-angle -60 > total-chemical-scent-in-cone 20 45 10]
      [uphill-chemical]]

Upvotes: 1

Views: 755

Answers (1)

Charles
Charles

Reputation: 4168

Your code has at least one extra bracket, after > total-chemical-scent-in-cone 20 45 10] in the first line. Indeed, why do you have a bracket before if and at the end of the last line? Is this expression part of a larger block of code? If not,

if chemical-scent-at-angle 0 > total-chemical-scent-in-cone 20 45 10 or chemical-scent-at-angle 60 > total-chemical-scent-in-cone 20 45 10 or chemical-scent-at-angle -60 > total-chemical-scent-in-cone 20 45 10
      [uphill-chemical]

should work. Personally, I find it clearer in long boolean expressions to use parentheses.

if (chemical-scent-at-angle 0 > total-chemical-scent-in-cone 20 45 10 or chemical-scent-at-angle 60 > total-chemical-scent-in-cone 20 45 10 or chemical-scent-at-angle -60 > total-chemical-scent-in-cone 20 45 10)
      [uphill-chemical]

I'm not clear as to what you are looking for in the second part of your question. An ifelse with multiple conditions in a single boolean would look the same,

ifelse (chemical-scent-at-angle 0 > total-chemical-scent-in-cone 20 45 10 or chemical-scent-at-angle 60 > total-chemical-scent-in-cone 20 45 10 or chemical-scent-at-angle -60 > total-chemical-scent-in-cone 20 45 10)
          [uphill-chemical]
          [do-something-else]

or, you can with NetLogo 6.1 and above, have multiple conditions, as in

(ifelse 
  a > 1 [do-something]
  b < 3 [do-something-else]
  c = 5 [do-yet-something-else]
  [default-to-this])

Note the parentheses around the whole thing.

Upvotes: 1

Related Questions