Browse By Unit
5 min readโขjune 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Aย nested iteration occurs when we have a loop inside of another loop, similar to nested conditional statements back in Unit 3. Here is the general anatomy:
loopOne {
loopTwo {
something
}
}
When a loop is nested inside another loop, all of the iterations of the inner loop must be completed until the next iteration of the outer loop starts. If there is a break statement inside the inner loop, then it only takes effect in the current iteration of the outer loop. When the next iteration of the outer loop starts, the inner loop starts all over again.
If there are two nested for loops without break statements, and the outer loop runs n times, and the inner loop runs m times for every iteration of the outer loop, then the inner loop will run m*n times. This can be extended to situations where there are more than 2 nested loops. The total number of times the innermost loop is run is the product of the number of times that each loop runs per iteration.
public static ArrayList<Integer> findNPrimes(int n) {ย
int prime = 2;
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
boolean notPrime = false;
while (!notPrime) {
for (int j = 2; j < prime; j++){
if (prime % j == 0) {
notPrime = true;
break;
ย }
ย ย ย ย ย ย ย }ย
ย ย ย ย ย ย ย if (notPrime) {
prime++;
notPrime = false;
} else {
notPrime = true;
}
}
primes.add(prime);
prime++;
}
return primes;
}
Explanation of Code
This is a nested iteration with three loops.ย
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
System.out.print("*");
}
System.out.println();
}
The code above will print out:
**
This is a nested iteration with two loops. In the first loop, we go from 0 toย n in order to haveย n rows in the output. In the second loop, we start from the row number and printย n-row number of stars for the row.ย
Once we exit the second loop, we print an empty line,ย i is incremented by one, and we enter the second loop to print the stars for the next line.ย
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
System.out.print(i+j);
}
System.out.println();
}
The code above will print out:
01234
2345
456
67
8
This is essentially the same code as earlier, except now we print out the sum of the row and column indices. This will allow us to demonstrate the use of break and continue in the next two examples.
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 3 && j == 3) {
break;
}
System.out.print(i+j);
}
System.out.println();
}
The code above will print out:
01234
2345
456
8
Notice that this time, because we told the program to break when it reaches the fourth row (whenย i = 3, since Java uses zero-based indexing) and tries to print the first number on that row (sinceย j is always set to the row index when we start a new row), it completely breaks out of the second loop so nothing is printed in the fourth row.ย
Once the program breaks out of the second loop, the first loop iterates by one and the program prints the fifth row the same way it did in the example above.
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 3 && j == 3) {
continue;
}
System.out.print(i+j);
}
System.out.println();
}
The code above will print out:
01234
2345
456
7
8
This time, instead of using a break, we used a continue. This means that we didn't completely break out of the second loop. We just skipped the iteration that happens whenย j is 3. (It doesn't print 6.) The second loop continues, iteratingย j to 4 and printing the 7. Thenย j is incremented again to be 5, but since that is not less thanย n (which is also 5), we exit the second loop normally.ย ย
Once we exit the second loop, we move on to the fifth row, which is printed normally.ย
Pay careful attention to the Break in Nested Loops example and the Continue in Nested Loops example, since they illustrate the important difference between break and continue. Break completely exits the loop it is called in; continue just skips the current iteration of the loop it is called in.
<< Hide Menu
5 min readโขjune 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Aย nested iteration occurs when we have a loop inside of another loop, similar to nested conditional statements back in Unit 3. Here is the general anatomy:
loopOne {
loopTwo {
something
}
}
When a loop is nested inside another loop, all of the iterations of the inner loop must be completed until the next iteration of the outer loop starts. If there is a break statement inside the inner loop, then it only takes effect in the current iteration of the outer loop. When the next iteration of the outer loop starts, the inner loop starts all over again.
If there are two nested for loops without break statements, and the outer loop runs n times, and the inner loop runs m times for every iteration of the outer loop, then the inner loop will run m*n times. This can be extended to situations where there are more than 2 nested loops. The total number of times the innermost loop is run is the product of the number of times that each loop runs per iteration.
public static ArrayList<Integer> findNPrimes(int n) {ย
int prime = 2;
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
boolean notPrime = false;
while (!notPrime) {
for (int j = 2; j < prime; j++){
if (prime % j == 0) {
notPrime = true;
break;
ย }
ย ย ย ย ย ย ย }ย
ย ย ย ย ย ย ย if (notPrime) {
prime++;
notPrime = false;
} else {
notPrime = true;
}
}
primes.add(prime);
prime++;
}
return primes;
}
Explanation of Code
This is a nested iteration with three loops.ย
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
System.out.print("*");
}
System.out.println();
}
The code above will print out:
**
This is a nested iteration with two loops. In the first loop, we go from 0 toย n in order to haveย n rows in the output. In the second loop, we start from the row number and printย n-row number of stars for the row.ย
Once we exit the second loop, we print an empty line,ย i is incremented by one, and we enter the second loop to print the stars for the next line.ย
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
System.out.print(i+j);
}
System.out.println();
}
The code above will print out:
01234
2345
456
67
8
This is essentially the same code as earlier, except now we print out the sum of the row and column indices. This will allow us to demonstrate the use of break and continue in the next two examples.
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 3 && j == 3) {
break;
}
System.out.print(i+j);
}
System.out.println();
}
The code above will print out:
01234
2345
456
8
Notice that this time, because we told the program to break when it reaches the fourth row (whenย i = 3, since Java uses zero-based indexing) and tries to print the first number on that row (sinceย j is always set to the row index when we start a new row), it completely breaks out of the second loop so nothing is printed in the fourth row.ย
Once the program breaks out of the second loop, the first loop iterates by one and the program prints the fifth row the same way it did in the example above.
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 3 && j == 3) {
continue;
}
System.out.print(i+j);
}
System.out.println();
}
The code above will print out:
01234
2345
456
7
8
This time, instead of using a break, we used a continue. This means that we didn't completely break out of the second loop. We just skipped the iteration that happens whenย j is 3. (It doesn't print 6.) The second loop continues, iteratingย j to 4 and printing the 7. Thenย j is incremented again to be 5, but since that is not less thanย n (which is also 5), we exit the second loop normally.ย ย
Once we exit the second loop, we move on to the fifth row, which is printed normally.ย
Pay careful attention to the Break in Nested Loops example and the Continue in Nested Loops example, since they illustrate the important difference between break and continue. Break completely exits the loop it is called in; continue just skips the current iteration of the loop it is called in.
ยฉ 2024 Fiveable Inc. All rights reserved.