Other variationsTopCalculating Interest ExampleKnitting a scarf: another example of nested For loops

Knitting a scarf: another example of nested For loops

We can rewrite the knitting example from earlier in the term, replacing the while loops with for loops as follows:

 // draws a scarf with upper left at point of click
    public void onMouseClick( Location point )  {
        double x = point.getX();
        double y = point.getY();    // x and y positions of the next ``stitch''
        
        for (int numRows = 0; numRows < LENGTH; numRows++) {
          for (int numCols = 0; numCols < WIDTH; numCols++) {
              new FramedOval(x, y, DIAMETER, DIAMETER, 
                             canvas).setColor(Color.green);
              x = x + X_DISP;
          }
            
          x = point.getX();
          y = y + Y_DISP;
        }
    }

Other variationsTopCalculating Interest ExampleKnitting a scarf: another example of nested For loops