# Java: Loop Exercises 4

Name: _________________________________
The questions contain code fragments. Assume that they occur in some legal context (appropriate declarations, etc).

1. Translate this for loop into a while loop.

          int sum = 0;
          for (int i = 1; i <= 5; i++) {
              sum = sum + i;
          }

2. Translate this while loop into a for loop.

          int stripes = 0;
          while (stripes < 13) {
              if (stripes%2 == 0) {
                  g.setColor(Color.RED);
              }else{
                  g.setColor(Color.WHITE);
              }
              g.fillRect(x, stripes*10, w, 10);
              stripes = stripes + 1;
          }

3. What does this display?

          for (int i=1; i<4; i++) {
              for (int j=3; j>0; j--) {
                  g.drawLine(100, 50*i, 200, 50*j);
              }
          }