Nested Loops
What You Will Learn
  • What is a nested loop
  • How to create and use a nested loop

Nested Loops

When a loop is located inside the body of another loop it is said to be nested within another loop. When you "nest" two loops, the outer loop takes control of the number of complete repetitions of the inner loop. While all types of loops may be nested, the most commonly nested loops are for loops.

for(int r = 0; r < 10; r++)      
// outer loop
{ for(int c = 0; c < 10; c++)
// inner loop
{ } }

We have all seen web page counters that resemble the one shown below. Your car's odometer works in a similar manner.

This counter and your car's odometer are little more than seven or eight nested for loops, each going from 0 to 9. The far-right number iterates the fastest, visibly moving from 0 to 9 as you drive your car or increasing by one as people visit a web site. A for loop which imitates the movement of the far-right number is shown below:

for(int ones=0; ones <= 9; ones++)
{
    System.out.println(ones);
}

The far-right number, however, is not the only number that is moving. All of the other numbers are moving also, but at a much slower pace. For every 10 numbers that move in the column on the right, the adjacent column is incremented by one. The two nested loops shown below may be used to imitate the movement of the two far-right numbers of a web counter or an odometer:

The number of digits in the web page counter or the odometer determine the number of nested loops needed to imitate the process.

When working with nested loops, the outer loop changes only after the inner loop is completely finished.

Example 1

The applet below demonstrates the flow of two nested for loops. Press the Step button to advance to the next statement. Press Reset to start the program over.

Example 2

A factorial of an integer, n, is the product of all the integers from 1 to n. It is written as n!. For example,

5! = 1 x 2 x 3 x 4 x 5 = 120
8! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 = 40320

It may seem a little strange if you haven't come across it before, but it can be a very useful value. For instance, n! is the number of ways you can arrange n different things in sequence, so a deck of cards can be arranged in 52! different sequences. Let's try calculating some factorial values.

The following example will calculate the factorial of every integer 1 up to a given limit.

public class Factorial
{
   public static void main(String[] args)
   {
      long limit = 20;      
      long factorial = 1;
	  
      // Loop from 1 to the value of limit
      for(int i =1; i <= limit; i++)                  // outer loop
      {
          factorial = 1;
          for(int factor = 2; factor <= i; factor++)  //  inner loop
          {
              factorial *= factor;
          }
          System.out.println(i + "!" + " is " + factorial);
      }
   }
}

This program will produces the following output

1! is 1
2! is 2
3! is 6
4! is 24
5! is 120
6! is 720
7! is 5040
8! is 40320
9! is 362880
10! is 3628800
11! is 39916800
12! is 479001600
13! is 6227020800
14! is 87178291200
15! is 1307674368000
16! is 20922789888000
17! is 355687428096000
18! is 6402373705728000
19! is 121645100408832000
20! is 2432902008176640000

The outer loop, controlled by i, walks through all the integers from 1 to the value of limit. In each iteration of the outer loop, the variable factorial is initialized to 1 and the nested loop calculates the factorial of the current value of i using factor as the control counter which runs from 2 to the current value of i. The resulting value of factorial is then displayed, before going to the next iteration of the outer loop.