Alan
Alan

Reputation: 9620

How does agent death surely affect the `while` loop that the agent is running?

As the following test illustrates, if a turtle is running a while when it dies, this breaks out of the while (without any error). Is this behavior guaranteed by NetLogo?

to test
  ca
  let age 0
  crt 1
  ask turtle 0 [
    while [true] [    
      set age (1 + age)
      print (word "age is " age)
      if (random-float 1 < 0.1) [die] ;death ends the while
    ]
  ]
end

Upvotes: 1

Views: 49

Answers (1)

Steve Railsback
Steve Railsback

Reputation: 1736

Yes- whenever a turtle executes "die" it immediately stops execution of any commands. In your case, the while loop (and "ask turtle 0") is terminated. The turtle is also automatically removed from all agentsets.

If you used "[die show "I'm dead!"]" instead of just "[die]", the code would never show "I'm dead!" because it stops as soon as it executes "die".

(If you have ever tried to program a model that removes agents in another programming language, you should appreciate how magical NetLogo's "die" primitive is. In other languages, doing the same thing can be a complicated nightmare.)

Upvotes: 3

Related Questions