Netlogo Nested Loops

Basically, my question is how nested loops work in netlogo. I tried nested two loops inside each other, but the inner one doesn't seem to behave properly (correctly, how they work in other languages). However, if I use two different loops, it works as expected. Perform loops in netlogo, similar to shortcuts in assembly language, so that it jumps to the first while it is marked with labels.

The reason I ask is because I resort to using different types of loops with each other or create a procedure for the inner loop, which impairs the readability of my code. Here is an example where I read 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 ji [ ifelse file-read = 1 [set pcolor white] [set pcolor black] ] set jj + 1 ] set ii - 1 ] file-close-all 

I want to remove the repeat 33 loop and have a while loop for use in different worlds.

+7
netlogo
source share
1 answer

Firstly, I wasn’t sure what you meant by β€œDo loops in netlogo work, like labels in assembler so that it jumps to the first while the label marks it,” but now I suspect you're referring to that if you nest two loop constructs in NetLogo, the possible use of stop breaks you out of the outer loop, which is admittedly not what you expect.

To demonstrate:

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

You will get this at the command center:

 observer> test-loop i1 j1 j2 j3 

I understand why this could be a problem. while in NetLogo , however, behaves as you expected:

 to test-while let i 0 while [ i <= 3 ] [ set ii + 1 type (word "i" i " ") let j 0 while [ j <= 3 ] [ set jj + 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 understand why you could not use two nested while .

+7
source share

All Articles