Learning For Loops: Code

// Hello! This is a for loop tutorial!
// You can copy and paste this on to a new processing sketch!
// By: aisencc
In a for loop you have 3 portions: (init; test; update){ stuff }
The value is first initiated, then tested, then the loop does “stuff”, and then it is updated.

An initiating value happens only once the first time the loop runs and it looks like:
int i= 0;
This initiating value is what your number count will start from. Usually we start counting from 0.
We use “i” because in math i is used sometimes in place for an intereger.
If we are using nested forloops we will initiate the value with “j” then “k” and so forth.
We could use “int ilovethisnumber = 0;” but that is just too long.

The test is much like the comparator tests we use in if statements, it looks like:
i < height;
i < 100;
This means that once i grows bigger than the height of our sketch the loop will stop.

The update is the opperation that allows the i to grow or shrink, it looks like:
i++; will add 1 everytime
i–; will subtract 1.
i+=10; adds 10 every time.
The update is last command to run after each turn of the loop,
and it will stop once the number is to high or too low and fails the test.

Run this sketch and see what happens.
Comment stuff out below, and run one forloop at a time to see which loop is drawing the lines you see

// remember that “//” allows you to comment things out, and not affect the code
// you can also use /* stuff */ to comment stuff out

void setup() {
size(600, 900); //window size
background(255); // white background
stroke(125, 125, 255); // color of our lines

for (int i = 0; i < height; i+=10) {
line(0, i, width, i);
for (int i = 0; i < width; i+=10) {
line(i, 0, i, height);

stroke(255, 30, 30);

for (int i = height/2; i < height; i = i+5) {
line(0, i, width, i);

for (int i = 40; i < 80; i = i+5) {
line(30, i, 80, i);
//// run this later! :)
//void draw() {
// stroke(0);
// for (int i = 0; i < height; i = i+90) {
// line(mouseX, i, mouseY, i);
// }
// for (int i = 0; i < width; i = i+90) {
// line(i, mouseX, i, mouseY);
// }

This entry was posted in Assignment, Uncategorized on by .

About aisencc

Hello! I'm Aisen Caro Chacin, my curiosity led me to research the intersecting fields of art, science, and technology driven by conceptual forms of inquiry and design thinking resulting in functional prototypes. I'm looking forward to seeing your ideas materialize. My inbox is always open.. so say "Hello!".

Leave a Reply