Help understanding nested loops.

The output of these loops is not what I expected.

A have an array of 4 ‘group’ structs that I iterate through with a for each loop. I print out the name of the group before I enter a While loop which iterates 4 times and prints “hello”.

I expected the output to be:

Group A
Hello
Hello
Hello
Hello
Group B
Hello
Hello
Hello
Hello
Group C
...

However the output is actually

Group A
Hello
Hello
Hello
Hello
Group B
Group C
Group D

What’s happening?

Your J is over 4 and is never rest…

1 Like

Hey @Nortksi!

MostHost_LA’s right, your while loop completes for the first pass but isn’t reset for the second one.

If you just add a Set J=0 here, it should fix the entire nested loop. :slight_smile:


Hope that helps, and keep on going! Logic is a great thing to be able to do!

Thanks guys, I knew the answer would be obvious :man_facepalming: