TopSummaryMore on loops

More on loops

Now that we have seen how important loops are in ActiveObject's, we would like to go back and discuss more complex loops. The first program generates a picture of a scarf wherever the user clicks.

The key to developing programs involving nested loops is to understand first what the outer loop is doing (going through successive rows) and what the inner loop is drawing (drawing all circles of an inner loop. We can think of the general structure as:

    while (rowNum < MAX_ROWS) {
       draw new row
       rowNum = rowNum+1
       shift y coordinate of new row
    }

Drawing a new roow is now requires only initializing the starting x coordinate and a while loop to draw all of the circles.

Each time we start a new row, there are a number of things that we will need to take care of:

  1. We need to reset the value of x so that we start drawing at the beginning of the row rather than where we left off.
  2. We to increase the value of y so that rows won't be drawn on top of each other.
  3. We need to reset numCols back to 0 so that it will keep the correct count when we restart drawing a row.
  4. We need to bump up numRows each time through.

TopSummaryMore on loops