If you wanted to draw 10 identical ellipses in a row, you could write 10 ellipse functions like so:

size(500, 300); //Declare and initialize variables int xPos = 30; int yPos = 150; int diameter = 40; int space = 49; // Draw 10 circles ellipse(xPos, yPos, diameter, diameter); ellipse(xPos + space, yPos, diameter, diameter); ellipse(xPos + 2 * space, yPos, diameter, diameter); ellipse(xPos + 3 * space, yPos, diameter, diameter); ellipse(xPos + 4 * space, yPos, diameter, diameter); ellipse(xPos + 5 * space, yPos, diameter, diameter); ellipse(xPos + 6 * space, yPos, diameter, diameter); ellipse(xPos + 7 * space, yPos, diameter, diameter); ellipse(xPos + 8 * space, yPos, diameter, diameter); ellipse(xPos + 9 * space, yPos, diameter, diameter);

Writing out 10 ellipse functions is not very hard, but what if you wanted to draw 200 ellipses or 1000 ellipses? It would be a lot of work to write all those ellipse functions (and even more work if you make a mistake in your calculations and have to correct every ellipse function).

Luckily, there is something called a while loop that allows us to draw as many ellipses as we want in just a few lines of code.

The text between the parentheses is called the

int counter = 0; int i = 0; while(counter < 10) { i += 5; counter++; }

When Processing gets to a while loop in a program, it first checks the loop conditional (in the example above, the loop conditional is counter < 10) to see if the statement is true. If it is true, it executes all the code in the loop body.

When Processing reaches the end bracket, it checks again to see if the conditional (counter < 10) is still true. If it is, Processing jumps back to the beginning bracket and processes all the code in the loop body.

As long as the conditional is true, Processing will continue to execute the code in the loop body. Processing can not exit the loop until the conditional is false.

int counter = 8; while (counter < 12) { println(“counter is: ” + counter); counter++; } println(“counter is greater than or equal to 12, exit the while loop”);

counter is: 9

counter is: 10

counter is: 11

counter is greater than or equal to 12, exit the while loop

size(500, 300); // Declare and initialize variables int counter = 0; int xPos = 30; // Draw 10 identical circles in a row while (counter < 10) { // Draw the ellipse ellipse(xPos, 150, 40, 40); // Shift the x-coordinate of the center of the circle // over by 49 pixels xPos += 49; // Increment the counter by 1 counter++;

## Leave a Reply