CS4ALL Computer Science For All Of Us
  • Home
  • Community
  • Workshop
    • Workshop Info
    • Schedule
    • Location
    • Sponsors
  • Resources
    • Scratch Resources
CS4ALL
  • Home
  • Community
  • Workshop
    • Workshop Info
    • Schedule
    • Location
    • Sponsors
  • Resources
    • Scratch Resources
  • Home
  • Processing
  • Beginner
  • While Loops

While Loops

July 10, 2016 Leave a Comment Written by Keith Atkinson

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

loop condition
. The loop condition must always be a boolean, which means that the loop condition must be true or false. The code between the curly brackets ( { } ) is called the loop body. The loop body is what gets executed if the loop condition is true.
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.

While loops can be difficult to understand at first.
It can be very helpful to walk through the code yourself line by line in order to understand what Processing is doing when it executes a while loop
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: 8
counter is: 9
counter is: 10
counter is: 11
counter is greater than or equal to 12, exit the while loop
Drawing Circles
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++;
Beginner, Processing
Bouncing Circle
For Loops

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *