Think of for loops as circles. Nested loops are like circles within circles.

Thankfully, you situation isn't nearly as complicated as this one. In this picture, the one large circle around is a for loop that is only true once. While inside that for loop, other loops were true multiple times. Each different size circle is a different loop.
I've rewritten your code with slightly more descriptive names, and with the sole purpose of explaining the loops - I took all the "useful" stuff out, and put console.writeline
statements instead.
// I'm using 5 as an example check here.
for (int outerLoopCounter = 0; outerLoopCounter < 5; outerLoopCounter++)
{
Console.WriteLine("OUTER: {0}", outerLoopCounter);
for (int middleLoopCounter = 0; middleLoopCounter < 5; middleLoopCounter++)
{
Console.WriteLine("MIDDLE: {0}", middleLoopCounter);
for (int innerLoopCounter = 0; innerLoopCounter < 5; innerLoopCounter++)
{
Console.WriteLine("INNER: {0}", innerLoopCounter);
}
}
}
The Output is as follows:
OUTER: 0
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
OUTER: 1
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
OUTER: 2
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
OUTER: 3
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
OUTER: 4
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
If you look at the indentation, you can see a pattern. The numbers closest to the text (to the left) are outputted from the outer loop. The middle ones come from the middle loop, and the furthest to the right numbers come from the innermost loop.
As you can see, the outer loop is slowly revolving, counting to four (0
, 1
, 2
, 3
, 4
), until it is no longer less than five, at which point it exits the loop. The middle circle (loop) is counting slightly faster. It gets to four once (again, 0-4) every time that the outer loop increments by one. And the inner circle is revolving quite quickly - it counts to four and exits each time the middle circle increments.