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 row 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:
More on loops |