EARTH 801
Computation and Visualization in the Earth Sciences

Lesson 2: Further examples of for loops III

PrintPrint

Increment

Here's an example of what happens when you change the increment variable from one loop to another, but leave the rest alone.

// for loop to demonstrate changing the increment 
size(400,100); 
pixelDensity(2); //makes prettier circles on a retina display 
int y = 20; //set the vertical position 
//this for loop makes a row of evenly-spaced circles 
for (int i =0; i< 400; i=i+50){ 
ellipse(i,y,20,20); 
} 
y=40; //shift the vertical position down 
//this for loop makes a row of circles with less space between them 
//because we changed the initialization variable 
for (int i=0; i<400; i=i+30){ 
ellipse(i,y,20,20); 
}

Screenshot of program in which a for loop's increment is changed
Screenshot of the output of the program above in which the looping increment is changed
program by E. Richardson in Processing

The only difference between the two loops is that I made the increment smaller in the second one. This results in more circles, spaced closer together. If I had made the increment bigger, there would be fewer circles spaced farther apart.

Negative increments

You can start at some initial value and work backwards to a lower value, as in the program below.

// for loop to demonstrate negative increments

size(400,100);
pixelDensity(2); //makes prettier circles on a retina display
int y = 20; //set the vertical position

//this for loop makes a row of evenly-spaced circles
//starting in the middle and going to the right
for (int i =200; i< 400; i=i+50){ 
ellipse(i,y,20,20);
}

y=40; //shift the vertical position down

//this for loop makes a row of circles
//starting in the middle and moving to the left
for (int i=200; i>0; i=i-50){
  ellipse(i,y,20,20);
}
annotated screenshot of program that has a loop with a negative increment, see text below
Annotated screenshot of the program above. The numbers in the circles tell you the order in which the loop plots them.
program by E. Richardson in Processing

In the first loop, we start at (200,20), and make evenly-spaced circles that shift to right by 50 pixels, until we get to x=400, then we stop. So the first loop is the equivalent of:

ellipse(200,20,20,20);
ellipse(250,20,20,20);
ellipse(300,20,20,20);
ellipse(350,20,20,20);

In the second loop, we start at (200,40), and make evenly-spaced circles that shift to the left by 50 pixels each time until we get to x=0. Note that not only do we have to change the increment, but we have to change the test portion of the loop as well in order to make this work. The second loop is the equivalent of:

ellipse(200,40,20,20);
ellipse(150,40,20,20);
ellipse(100,40,20,20);
ellipse(50,40,20,20);

Beware the infinite loop!

When you set up a for loop pay attention so that you don't write a loop that will never terminate. For example, what if I wrote the following loop:

for (int i = 10; i < 200; i = i - 10)

Bad trouble! Why? The initial value of i is 10, and then I am decreasing the value of i by 10 in the increment portion. But, my test checks whether or not the value of i is less than 200 and if it is, to go ahead and execute the loop. The value of i will never exceed 200 since i is getting more negative all the time, so this loop will never end because the test will never fail.