2. Nested For Loops

2.1. Objectives

  • Iteration using nested for loops

2.2. Time Goal

  • 20 minutes on this section

2.3. Key Terms

2.4. Exercises

Note

A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns as shown below.

Note

The number of times a nested for loop body is executed is the number of times the outer loop executes times the number of times the inner loop executes. For the example above the outer loop executes 4-0+1= 5 times and the inner 9-0+1=10 times so the total is 5 * 10 = 50.

    Q-115: How many times does the following code print a *?

    for (int i = 3; i < 8; i++)
    {
        for (int y = 1; y < 5; y++)
        {
            System.out.print("*");
        }
        System.out.println();
    }
    
  • 40
  • This would be true if the outer loop executed 8 times and the inner 5 times, but what is the initial value of i?
  • 20
  • The outer loop executes 7-3+1=5 times and the inner 4-1+1=4 so this will print 5 * 4 = 20 stars.
  • 24
  • This would be true if the outer loop executed 6 times such as if it was i <= 8.
  • 30
  • This would be true if the inner loop executed 5 times such as if it was y <= 5.

    Q-116: What does the following code print?

    for (int i = 2; i < 8; i++)
    {
        for (int y = 1; y <= 5; y++)
        {
            System.out.print("*");
        }
        System.out.println();
    }
    
  • A rectangle of 8 rows with 5 stars per row.
  • This would be true if i was initialized to 0.
  • A rectangle of 8 rows with 4 stars per row.
  • This would be true if i was initialized to 0 and the inner loop continued while y < 5.
  • A rectangle of 6 rows with 5 stars per row.
  • The outer loop executes 8-2+1=6 times so there are 6 rows and the inner loop executes 5-1+1=5 times so there are 5 columns.
  • A rectangle of 6 rows with 4 stars per row.
  • This would be true if the inner loop continued while y < 5.

    Q-117: What does the following print?

    for (int i = 3; i <= 9; i++)
    {
       for (int j = 6; j > 0; j--)
       {
           System.out.print("*");
       }
       System.out.println();
    }
    
  • A rectangle of 9 rows and 5 stars per row.
  • Did you notice what i was initialized to?
  • A rectangle of 6 rows and 6 stars per row.
  • It would print 6 rows if it was i < 9.
  • A rectangle of 7 rows and 5 stars per row.
  • It would print 5 stars per row if it was j > 1.
  • A rectangle of 7 rows and 6 stars per row.
  • The outer loop executes 9 - 3 + 1 = 7 times and the inner 6 - 1 + 1 = 6 times.

    Q-118: How many stars are output when the following code is executed?

    for (int i = 0; i < 5; i++) {
       for (int j = 0; j < 5; j++)
          System.out.println("*");
    }
    
  • 10
  • The second loop executes 5 times for each of the 5 times the first loop executes, so the answer should be 5 * 5.
  • 5
  • The second loop executes 5 times for each of the 5 times the first loop executes, so the answer should be 5 * 5.
  • 25
  • The first loop will execute 5 times, and for each time through, the second loop will execute 5 times. So the answer is the number of times through the first loop times the number of times through the second.
  • 50
  • The second loop executes 5 times for each of the 5 times the first loop executes, so the answer should be 5 * 5.
  • 15
  • The second loop executes 5 times for each of the 5 times the first loop executes, so the answer should be 5 * 5.

    Q-119: Which of the following code segments will produce the displayed output?

    1
    22
    333
    4444
    55555
    
    
    I.   for (int i = 1; i <= 5; i++) {
            for (int j = i; j > 0; j--) {
               System.out.print(i);
            }
            System.out.println();
         }
    
    II.  for (int i = 0; i < 5; i++) {
            for (int j = 0; j < i; j++) {
               System.out.print(i);
            }
            System.out.println();
         }
    
    III. for (int i = 1; i < 5; i++) {
            for (int j = i; j > 0; j--) {
               System.out.print(i);
            }
            System.out.println();
         }
    
    IV.  for (int i = 1; i < 6; i++) {
            for (int j = 0; j < i; j++) {
               System.out.println(i);
            }
         }
    
    V.   for (int i = 0; i < 5; i++) {
            for (int j = 0; j < i; j++) {
               System.out.print(i+1);
            }
            System.out.println();
         }
    
  • I
  • This will loop with i changing from 1 to 5 and then for each i, j will loop from i to 0 printing the value of i and then a new line.
  • II
  • This will loop i from 0 to 4 and j from 0 to i, neglecting to ouput 5.
  • III
  • This will loop with i changing from 1 to 4 and j from i to 0.
  • IV
  • This will loop with i changing from 1 to 5 and j from 0 to i but it will print each value on a different line.
  • V
  • This will loop with i changing from 0 to 4 and j from 0 to i.

    Q-120: What is printed as a result of the following code segment?

    for (int k = 0; k < 20; k+=2) {
       if (k % 3 == 1)
          System.out.println(k + " ");
    }
    
  • 0 2 4 6 8 10 12 14 16 18
  • This would be correct if we were printing out all of the values of k, not just the ones that have a remainder of 1 when divided by 3.
  • 4 16
  • This is missing the value 10 (10 divided by 3 does have a remainder of 1).
  • 0 6 12 18
  • None of these answers have a remainder of 1 when divided by 3.
  • 1 4 7 10 13 16 19
  • This answer would be correct if k was incremented by 1 instead of 2. K will be 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 in this loop.
  • 4 10 16
  • This will loop with k having a value of 0 to 18 (it will stop when k = 20). It will print out the value of k followed by a space when the remainder of dividing k by 3 is 1.

    Q-121: Which of the following code segments will produce the displayed output?

    11111
    2222
    333
    44
    5
    
    
    I.   for (int j = 1; j <= 5; j++) {
            for (int k = 5; k >= j; k--) {
               System.out.print(j + " ");
            }
            System.out.println();
         }
    
    II.  for (int j = 1; j <= 5; j++) {
            for (int k = 5; k >= 1; k--) {
               System.out.print(j + " ");
            }
            System.out.println();
         }
    
    III. for (int j = 1; j <= 5; j++) {
            for (int k = 1; k <= j; k++) {
               System.out.print(j + " ");
            }
            System.out.println();
         }
    
    IV.  for (int j = 1; j <= 5; j++) {
            for (int k = 1; k <= 5; k++) {
               System.out.println(j + " ");
            }
         }
    
    V.   for (int j = 1; j <= 5; j++) {
            for (int k = j; k <= 5; k++) {
               System.out.print(k + " ");
            }
            System.out.println();
         }
    
  • I
  • This will loop with j from 1 to 5 and k from 5 to j and print out the value of j and a space. So the first time through the loop it will print 1 five times and the next time it will print out 2 four times and so on.
  • II
  • This will print out each value from 1 to 5 five times.
  • III
  • This will loop with j from 1 to 5 and k from 1 times.
  • IV
  • This will loop j from 1 to 5 and k from 1 to 5, printing each number 5 times.
  • V
  • This loops with j from 1 to 5 and k from j to 5 and prints out the value of k, printing 1 through 5 on the first line, 2 through 5 on the next, and so on.
Next Section - 3. Arrays in Java