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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With