2.11 Nested Iteration
- Develop code to represent nested iterative processes and determine the result of these processes
Assignment
- All vocabulary
- All 2.11 activities
- Loops Coding Practice (2.7-2.12)
Loops are good working in one dimension, but what about second dimension? Putting a loop inside another loop will get that done. Before doing anything in the book, I suggest running the loop below and make sure you understand how it works.
1
2
3
4
5
6
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= 5; j++) {
System.out.print("(" + i + ", " + j + ")\t");
}
System.out.println();
}
The outer loop increments after the inner completely finishes, so the pattern of what happens is
- Inner loop begins with
i = 0andj = 0 - Inner loop finishes with
i = 0andj = 5 - Outer loop updates with
i++ - Inner loop begins with
i = 1andjis redeclared and reset to0
Variables are local to block where they are declared. Since j is declared in the inner loop, it cannot be used outside of it. It’s redeclared and reset to zero every time that loop restarts.