4c – for loops

Programmers are inherently lazy, and we want complex results for relatively little code. How would we go about drawing 1000 lines? We could spend hours in illustrator, we could write the function for line out 1000 times. Or we could use what is known as a for loop statement.

Screen Shot 2015-02-16 at 10.51.50 PM

For Statements have three parameters: where a variable starts, where it ends, and how much it iterates by. The program will loop through each iteration of the variable in the for loop before it continues on with the rest of the program.

Screen Shot 2015-02-16 at 10.52.09 PM
The following code shows us a for loop in action:

 

void setup(){
  size(600, 600); 
  smooth(); 
}
 
void draw(){
  for (int i = 0; i < width; i += 20){
    ellipse(i, 20, 20, 20); 
  } 
}

Given this example, can you create the image below?

dots

Exercise

1. Develop a composition which in which one straight lines respond to the cursor.
2. Develop a composition which in which ten straight lines respond to the cursor.
3. Develop a composition which in which one thousand straight lines respond to the cursor.

'+
1
'+
2 - 3
4 - 5
6 - 7
8 - 9
10 - 11
12 - 13
13 - 14
[x]