In the previous lab, we discussed selection. In this lab, we’ll work with iteration, the third of the three basic control structures. When you built animations, you were implicitly using iterative behavior, but Processing did the iteration for you. In this lab you’ll program your own iteration.

The Counting Loop

Processing provides statements that support a number of forms of iteration. One common form is the counting for loop.

void setup() {
  size(200, 150);
  background(255);
  strokeWeight(3);
  noLoop();
}

void draw() {
  // Draw a dotted line manually.
  point(0, 50); point(10, 50); point(20, 50); point(30, 50); 
  point(40, 50); point(50, 50); point(60, 50);  // ...
  
  // Draw a dotted line with a loop.
  for (int i = 0; i < width; i+=10) {
    point(i, 100);
  }

}

Flowchart:

Pattern:

for (initialization; loopCondition; stepExpression)
 statement
  • initialization - This initialization expression is evaluated before entering the loop. It generally declares and initializes a counting variable.
  • loopCondition - This boolean expression is evaluated before each iteration. If it is true, then the statement is executed; if it is false, the loop terminates.
  • stepExpression - This expression is evaluated at the end of each iteration. It generally increments the counter variable.
  • statement - The statement or block is executed on each iteration.

This code draws dotted lines, manually and then with a counting loop. The manual version is a hopelessly tedious task of setting the coordinates for each point separately. The counting for loop provides a more compact way of expressing processes that involve counting. Note that this program uses the noLoop() statement to tell processing to turn off its iterative calls to draw(). That allows it to implement its own non-animated loops.

Using Loops in Methods

void setup() {
  size(200, 250);
  background(255);
  strokeWeight(3);
  noLoop();
}

void draw() {
  drawDottedLine(25, 50);
  drawDottedLine(10, 100);
  drawDottedLine(5, 150);
  drawDottedLine(1, 200);
}

void drawDottedLine(int increment, int y) {
  for (int i = 0; i < width; i+=increment) {
    point(i, y);
  }
}

This code draws a number of dotted lines, culminating in a solid line made from points. The drawDottedLine() method uses parameters to encapsulate the essence of drawing a dotted line.

Loops Behaving Badly

When designing and implementing iteration, be careful to ensure that your loops terminate. A non-terminating loop is called an infinite loop. Sometimes, we’d like our loops to go forever, as is the case with operating systems, but usually we need them to terminate in a “reasonable” amount of time.

Beware, it can be difficult to stop an out-of-control loop once it has started running.

Exercise 3.f.1. What will the following counting loops do? Think first before running these loops, some of them may be infinite loops.
  1. for (int i = 1; i <= 10000; i++) {
      print(i + " ");
    }
    
  2. for (int i = 1; i > 0; i++) {
      print(i + " ");
    }
    
  3. for (int i = 1; i > 10; i++) {
      print(i + " ");
    }
    
  4. for (int i = 1; i > 10; i = i + 0) {
      print(i + " ");
    }
    
Record your answers so that you can turn them in later.

Using Loops with other Figures

We can draw other figures using loops.

Exercise 3.f.2. Write counting for loops that draw the following figures:

Save the looping code you used to create each of these examples.

Data Structures and Algorithms

Now that you understand the three basic control structures, you are ready to use them in combination to design your own computations. Computations are a combination of two things:

As the goals for your programs become more complicated, it becomes valuable to design your data structures and algorithms before you start programming. It can then implement them by translating them into programs that the computer can understand and execute. Note that you algorithms must be carefully crafted; computers may be fast, but they are notoriously dumb in a number of ways:

Exercise 3.f.3. Write a set of instructions for distributing a brownie to each student in the class who wants one. Note the following:

Checking In

Submit your code for the lab exercises and include a screen capture for each.