8

Basically, my question is how nested loops work in netlogo. I've tried nesting two while loops within each other, but the inner one doesn't seem to behave properly (properly being the way they work in other languages). If I use two different loops though, it works as expected. Do loops in netlogo work similar to labels in assembly language, so that it'll jump to the first while label it notices.

The reason I'm asking is because I've been resorting to using different types of loops with each other, or creating a procedure for the inner loop, which is hurting the readability of my code. Here's an example, where I'm reading in a tab delimited file to assign values to patches:

file-open "map2.txt"
let i max-pycor
let j min-pxcor
while [i >= min-pycor] [
 repeat 33 [ask patch j i [
    ifelse file-read = 1
      [set pcolor white]
      [set pcolor black]
    ]
    set j j + 1
  ]
  set i i - 1
]
file-close-all

I want to remove that repeat 33 loop and have a while loop to use with variably sized worlds.

dragmosh
  • 393
  • 3
  • 12
  • In general, you should post the code that is posing you a problem instead of what you did to work around it. It would make it easier to figure out what went wrong... – Nicolas Payette Nov 28 '13 at 22:47
  • 1
    Also: you know about [`world-width`](http://ccl.northwestern.edu/netlogo/docs/dictionary.html#world-width), right? That could have been used instead of `33` in your code... – Nicolas Payette Nov 28 '13 at 23:58
  • @NicolasPayette Didn't know about world-width, I'll now be able to remove max-pycor - min-pycor from so many places. Thanks much! – dragmosh Nov 29 '13 at 08:10

1 Answers1

8

At first, I was not sure what you meant by "Do loops in netlogo work similar to labels in assembly language, so that it'll jump to the first while label it notices", but I now suspect that you are referring to the fact that if you nest two loop constructs in NetLogo, the eventual use of stop breaks you out of the outer loop, which is admittedly not what you would expect.

To demonstrate:

to test-loop
  let i 0
  loop [
    set i i + 1
    type (word "i" i " ")
    let j 0
    loop [
      set j j + 1
      type (word "j" j " ")
      if j = 3 [ stop ]
    ]
    if i = 3 [ stop ]
  ]
end

Will give you this in the command center:

observer> test-loop
i1 j1 j2 j3 

I see why this could be a problem. while in NetLogo, however, behaves just as you would expect it:

to test-while
  let i 0
  while [ i <= 3 ] [
    set i i + 1
    type (word "i" i " ")
    let j 0
    while [ j <= 3 ] [
      set j j + 1
      type (word "j" j " ")
    ]
  ]
end

And in the command center:

observer> test-while
i1 j1 j2 j3 j4 i2 j1 j2 j3 j4 i3 j1 j2 j3 j4 i4 j1 j2 j3 j4

...so I don't see why you could not use two nested while loops.

Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
  • Thanks, there must've been a different problem that kept dropping me out of the inner loops, cause this happened in more than one place. I need to keep track of my loop checks better! – dragmosh Nov 29 '13 at 08:08