Intro to nested loops  

  • Using a loop-statement, a program can iterate over a series of values

    Example:

       int i;
    
       for ( i = 1; i <= 10; i++ )
       {
           // Loop goes through the series of value 1, 2, 3, ..., 9, 10
       }
    

  • There are higher dimensions of sets of values

    Example: a 2-dimensional grid of values

       (1,1)  (1,2)  (1,3)  (1,4)
       (2,1)  (2,2)  (2,3)  (2,4)
       (3,1)  (3,2)  (3,3)  (3,4)
    

  • Nesting loop-statements enables a computer program to process higher dimensional sets of values

Nested loops

  • Nested loop statement:

    • Nested loop statement = when another loop statement is used inside a loop statement

    Example:

     for ( i = 1; i < 10; i++ )        // Outer loop
     {
        for ( j = 1; j < 10; j++ )     // Inner loop
            System.out.println( i + " " + j );
     } 

  • The first loop (for ( i = 1; ... ) is called:

    • the outer loop

  • The second loop (for ( j = 1; ... ) is called:

    • the inner loop

The execution of nested loops

  • Nested loops are executed as follows:

    • Each time the outer loop starts a new iteration:

      • the inner loop is re-entered, and run again.

    Example:

       public static void main(String[] args) 
       {
          int i, j;
          
          for ( i = 1; i <= 3; i++ )
          {
             for ( j = 1; j <= 4; j++ )
                System.out.println( i + " " + j );
          }
       } 

DEMO: demo/05-loops/06-nested-loop/Demo1.java       Step the program in BlueJ

  Example nested loop:    print the multiplication table for numbers 1 to 9  

Write a Java program that prints the multiplication table for the number 1 to 9


         1   2   3   4   5   6   7   8   9
         2   4   6   8  10  12  14  16  18
	 3   6   9  12  15  18  21  24  27
	 4   8  12  16  20  24  28  32  36
	 5  10  15  20  25  30  35  40  45
	 6  12  18  24  30  36  42  48  54
	 7  14  21  28  35  42  49  56  63
	 8  16  24  32  40  48  56  64  72
	 9  18  27  36  45  54  63  72  81
 

Code:







  Example nested loop:    print the multiplication table for numbers 1 to 9  

The program will print 1 line of the table in each outer iteration:


 i = 1   1   2   3   4   5   6   7   8   9   <--- print this line in iter 1
 i = 2   2   4   6   8  10  12  14  16  18   <--- print this line in iter 2
 i = 3	 3   6   9  12  15  18  21  24  27   <--- print this line in iter 3
 i = 4	 4   8  12  16  20  24  28  32  36   <--- print this line in iter 4
 i = 5	 5  10  15  20  25  30  35  40  45   <--- print this line in iter 5
 i = 6	 6  12  18  24  30  36  42  48  54   <--- print this line in iter 6
 i = 7	 7  14  21  28  35  42  49  56  63   <--- print this line in iter 7
 i = 8	 8  16  24  32  40  48  56  64  72   <--- print this line in iter 8
 i = 9	 9  18  27  36  45  54  63  72  81   <--- print this line in iter 9
 

Code:

  for ( i = 1; i <= 9; i++ )  // Program will repeat for i = 1, 2, 3, ... 9
  {

      // print line i

  }

  Example nested loop:    print the multiplication table for numbers 1 to 9  

The numbers on line i are equal to:   1*i   2*i   3*i   4*i   5*i   6*i   7*i   8*i   9*i:


 i = 1   1   2   3   4   5   6   7   8   9   <--- print this line in iter 1
 i = 2   2   4   6   8  10  12  14  16  18   <--- print this line in iter 2
 i = 3	 3   6   9  12  15  18  21  24  27   <--- print this line in iter 3
 i = 4	 4   8  12  16  20  24  28  32  36   <--- print this line in iter 4
 i = 5	 5  10  15  20  25  30  35  40  45   <--- print this line in iter 5
 i = 6	 6  12  18  24  30  36  42  48  54   <--- print this line in iter 6
 i = 7	 7  14  21  28  35  42  49  56  63   <--- print this line in iter 7
 i = 8	 8  16  24  32  40  48  56  64  72   <--- print this line in iter 8
 i = 9	 9  18  27  36  45  54  63  72  81   <--- print this line in iter 9
 

Code:

  for ( i = 1; i <= 9; i++ )  // Program will repeat for i = 1, 2, 3, ... 9
  {
      for ( j = 1; j <= 9; j++ )
          System.out.print("  " + j*i);   // prints 1*i  2*i  3*i ... 9*i
      System.out.println();               // prints "empty line" (go to next line)
  }

DEMO: demo/05-loops/06-nested-loop/MultTable.java

  Example nested loop:    print the multiplication table for numbers 1 to 9  

The complete Java program is:

   public static void main(String[] args) 
   {
      int i, j;

      for ( i = 1; i <= 9; i++ )
      {
          for ( j = 1; j <= 9; j++ )
              System.out.print("  " + i*j);
          System.out.println();
       }
   } 

Improvement:   use System.out.printf("%4d", i*j) to print i*j in 4 characters places:

  for ( i = 1; i <= 9; i++ )  // Program will repeat for i = 1, 2, 3, ... 9
  {
      for ( j = 1; j <= 9; j++ )
          System.out.printf("%4d" , i*j); // prints 1*i  2*i  3*i ... 9*i
      System.out.println();               // prints "empty line" (go to next line)
  }

DEMO: demo/05-loops/06-nested-loop/MultTable2.java