or
is a logic primitive that takes two conditional statements and reports true
if either is true. In other words, it will report true
if 1) the first condition is true but the second is false, 2) the second condition is true but the first is false, or 3) both conditions are true. or
will report false
only if both of the conditions are false. For example, if we wanted to create a model of a predator-prey ecosystem where deer only move if there was no grass nearby or if there was a wolf nearby, we would write the following code:
ask deer [
if grass-here = 0 or any? wolves in-radius 2 [
move
]
]
Things to keep in mind when using or
:
or
statements if we want to check more than just two conditions such as if color = red or size < 2 or [ ... ]
. In such a statement, if only just one of the statements reports true
, the whole statement will report true
.and
is a primitive that does the exact opposite of or
; it reports true
only if both conditions are true.xor
primitive.In the model example below, we have some plants and some sheep. Sheep move around randomly and they eat a plant if it is either green or
yellow. A magenta plant indicates a poisonous one, so sheep don't eat.
xxxxxxxxxx
breed [animals animal]
breed [plants plant]
to setup
clear-all
ask patches [
set pcolor brown
]
create-animals 30 [
set shape "sheep"
set color white
setxy random-xcor random-ycor
]
create-plants 100 [
set shape "plant"
set color one-of [green lime]
setxy random-xcor random-ycor
]
ask n-of 20 plants [
set color one-of [yellow magenta]
]
reset-ticks
end
to go
ask animals [
wiggle
forward 0.5
let edible-plants (plants-here with [color = green or color = lime])
let poison-plants (plants-here with [color = yellow or color = magenta])
if any? edible-plants [
ask edible-plants [ die ]
]
if any? poison-plants [
ask poison-plants [ die ]
die
]
]
tick
end
to wiggle
left random 90
right random 90
end
Once you mastered the or
primitive, don't stop there. Check out the resources below to improve your NetLogo skills.
or
primitive:if
Carries out a provided set of rules (code) if a given condition is true. Does nothing if a given condition is false.