question

Laurenz Peleman avatar image
0 Likes"
Laurenz Peleman asked Laurenz Peleman answered

Strange result using continue in (nested) for loop

Hi everyone

I came around an unexpected result when using the "continue;" like in the following code snippet. The aim of the code is to execute <other code>, for all objects connected to the centerports of "current" , in case their content is non-zero. If the object is empty, the code block can be skipped and the next (second) connected object should be evaluated.

for (int i  = 1;i <= 2;i++) {
     for (int j = 1;j <= 2;j++) {
	if (current.centerObjects[j].subnodes.length  == 0) {
          continue;
     	}          
	<other code>
     }
     <...>
}

Now, when the content of the Object current.centerObjects[1] is 0, so the if block is accessed and the continue statement is called, both the inner and outer for loop stop iterating: only the case i = 1, j = 1 is executed, none of the next iterations (i = 1,j = 2 and i = 2). Simultaneously, I get a Flexscript exception in the System console.

I know I could work around the continue statement by redefining the if expression and put the code in that new block, but this demonstrated that I do not truly understand how "continue" works in a for loop. Hopefully, someone can give me this insight.

Many thanks in advance

Choose One
continue
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Cameron Pluim avatar image
1 Like"
Cameron Pluim answered

@Laurenz Peleman In order to understand how continues work, it's best to put in print statements, or pt(), in various places to see what is happening. In your case, let's modify your code slightly to look like this:

for (int i  = 1;i <= 2;i++) {
    for (int j = 1;j <= 2;j++) {
        if (i == 1) {
            pt("Continue i:" + numtostring(i) + " j: " + numtostring(j));pr();
	    continue;
        }          
        pt("J-Iteration end i:" + numtostring(i) + " j: " + numtostring(j));pr();
    }
    pt("I-Iteration end i:" + numtostring(i));pr();
}

This way we can tell which iteration we are on, and what is going to happen at each. With this code, the output console prints out the following:

Continue i:1 j: 1
Continue i:1 j: 2
I-Iteration end i:1
J-Iteration end i:2 j: 1
J-Iteration end i:2 j: 2
I-Iteration end i:2

So the continue will force the innermost for loop to start it's next iteration, and not execute any of the rest of the code in that for loop following the continue.

Hopefully this helps.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Laurenz Peleman avatar image
0 Likes"
Laurenz Peleman answered

@Cameron Pluim Thanks for the elaborated explanation, it seems that the continue command indeed works like I expected. I found the culprit of the strange output and the exception in the System console. In the <other code>-block I create an array using makearray with current.centerObjects[j].subnodes.length elements. Any time the Object current.centerObjects[j] is empty, the error message is generated, the iterations of that loop are cut off and the model proceeds to the next loop. Hence, although the continue statement should have resulted in skipping the code block containing this Array assignment, apparently there must have been at least 'some' evaluation of these statements.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.