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. Last time we looked at a program that generates a picture of a scarf wherever the user clicks.
Here are a few things that we mainly discussed Wednesday, but we'll include them in the notes for today anyway.
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:
Suppose we wish to design a class to draw an American flag.
New things appearing in the code:
Old, but interesting, things in the code include the nested while loop in the drawStars method as well as the fact that several items are changing each time through the while loops. Notice that in the outer while loop in drawStars, we must reinitialize both col and x each time through the loop.
More on loops |