Repeating a loop?

 

Let’s start out with a code segment.  Consider what the following segment does:

 

      Board b = new Board(10,5);

   int x = 0;

   while (x < 10)

   {

      b.setChar(x, 0, ‘A’);

      b.pause(500);

      x++;

   }

 

It first create a Board object with 10 columns and 5 rows.  In the loop, the variable x represents the x coordinate values of the cells in which the character ‘A’ is written.  Since the Board object is 10 cells wide, the value of x starts at 0 and end at 9.  The y coordinate, represented by the second parameter to setChar, is 0, which tells us that we will be writing ‘A’s in the top row (row 0).  If we were to place A’s in the second row instead, we would have needed to change the second parameter of setChar to 1:

 

            b.setChar(x, 1, ‘A’);

 

Suppose we’d like to place ‘A’s in all the rows of the board.  One way to look at this is as a repetition of the execution of the code segment above (the writing of A’s in a row) for all the rows in the Board object (this is suspiciously sounding like we’re going to repeat the execution of a loop).  We already know that in order to repeat the execution of a statement we place it inside of a loop, so if we’d like the execution of a loop to be repeated, the loop itself must be placed inside of another loop.  The first loop would be the inner loop and the second the outer loop.  The notion of nested loops is born.  Let’s go back to our problem.   The question is to figure out how many times we’d like to repeat the loop’s execution.  Since we have 5 rows, that number must be 5.  Let’s create the outer loop such that it executes 5 times.   It makes sense to call the loop’s condition variable y since it corresponds to the different rows.

 

      Board b = new Board(10,5);

      int y = 0;

 

      // as long as the current row is less than 5 (max value of 4)

      while (y < 5)

      {

            // the inner loop places A’s in the row numbered y

            int x = 0;

            while (x < 10)

            {

                  // both the row and column numbers are variables

                  b.setChar(x, y, ‘A’);

                  b.pause(500);

                  x++;

            }

            // after the inner loop executes, go to the next row

            y++;

      }

 

Suppose we’re asked to modify the above code so that it works for a Board object of a different size, say a 15x12.  This would mean that we need to change the parameters in the first line to 15 and 12, and change the condition statements for each of the loops.  The problem is that the more frequently we refer to the board dimensions using values in our program the more changes we will have to make to our program.  This is a recipe for errors in our program since it becomes easier to miss one of the changes the more changes we have to make.  An easy way to get around the problem is to have the dimensions of the board stored in variables that are initialized to the values that we want.  Every reference to the dimensions from this point on will be using the variables, not values.  If we’d like the program to work for a board with different dimensions, we simply need to change the initial values of the variables cols and rows – everything else is unchanged.  Here’s the full program: 

 

  public class Animate3

  {

    public static void main(String[] args)

    {

      // only the initial values need to change for a

      // different size board

      int rows = 10;

      int cols = 5;

      Board b = new Board(cols, rows);

   

      int y = 0;

      // loop around as long as y is less than rows

      while (y < rows)

      {

        int x = 0;

        while (x < cols)

        {

          b.setChar(x, y, 'A');

          b.pause(500);

          x++;

        }     

        y++;

      }

    }

  }