What is a nested loop?
A loop inside another loop.
What is the outer loop in a nested loop?
The loop that controls the overall iterations.
What is the inner loop in a nested loop?
The loop that executes completely for each cycle of the outer loop.
What does the `break` statement do in a nested loop?
Exits the inner loop immediately.
What does the `continue` statement do in a nested loop?
Skips the current iteration of the inner loop.
What does the following code output?
```java
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();
}
}
printPyramid(3);
```
***
**
*
What does the following code output?
```java
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();
}
}
printPyramid(3);
```
012
23
4
What does the following code output?
```java
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 1 && j == 1) {
break;
}
System.out.print(i+j);
}
System.out.println();
}
}
printPyramid(3);
```
012
2
4
What does the following code output?
```java
public static void printPyramid(int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (i == 1 && j == 1) {
continue;
}
System.out.print(i+j);
}
System.out.println();
}
}
printPyramid(3);
```
012
23
4
Identify the potential issue in the following code:
```java
for (int i = 0; i < n; i++) {
for (int j = 0; i < m; j++) {
System.out.println(i + " " + j);
}
}
```
The inner loop condition `i < m` should be `j < m` to avoid an infinite loop or incorrect behavior.
What will be the value of `primes` after the following code executes with `n = 3`?
```java
public static ArrayList<Integer> findNPrimes(int n) {
int prime = 2;
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < n; i++) { // Outer loop: Finds n primes
boolean notPrime = false;
while (!notPrime) { // Middle loop: Checks if current number is prime
for (int j = 2; j < prime; j++){ // Inner loop: Checks for divisors
if (prime % j == 0) {
notPrime = true;
break; // Exit inner loop if not prime
}
}
if (notPrime) {
prime++;
notPrime = false;
} else {
notPrime = true; // Exit middle loop if prime
}
}
primes.add(prime);
prime++;
}
return primes;
}
ArrayList<Integer> primes = findNPrimes(3);
```
[2, 3, 5]
What is the output of the following code snippet?
```java
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] % 2 == 0) {
System.out.print(matrix[i][j] + " ");
}
}
}
```
2 4 6 8
What is the output of this code?
```java
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}
```
1 2 3
2 4 6
3 6 9
What is the output of the following code?
```java
int count = 0;
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
count++;
}
}
System.out.println(count);
```
10
What is the output of the following code snippet?
```java
int x = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
x += i + j;
}
}
}
System.out.println(x);
```
6
How many times does the inner loop execute if the outer loop runs 'n' times and the inner loop runs 'm' times?
n * m times.
What is the first step in the execution order of nested loops?
The outer loop starts.
What is the second step in the execution order of nested loops?
The inner loop starts and runs to completion.
What is the key to understanding the output of nested loops?
Pay close attention to how the loop variables change in both the inner and outer loops.
How do nested loops affect time complexity?
Nested loops often result in O(n^2) or worse time complexity.
What is the primary use case of nested loops?
Iterating over elements in 2D arrays or matrices.
What is the role of the outer loop in printing patterns?
Controls the number of rows.
What is the role of the inner loop in printing patterns?
Controls the elements (e.g., stars, numbers) printed in each row.
What is the impact of `break` on the outer loop?
The outer loop continues to the next iteration.
What is the impact of `continue` on the outer loop?
The outer loop continues to the next iteration.