Loops
To complete a loop you must complete something you want to happen over and over again. So what you want to do is complete something you want to happen over and over again, So what you want to do is complete something you want to happen over and over again So what you want to do is complete something you want to happen over and over again So what you want to do is complete something you want to happen over and over again So what you want to do is complete something you want to happen over and over again..ha ha
This fancy thing is actually called an ITERATION. Imagine the following code
This fancy thing is actually called an ITERATION. Imagine the following code
Imagine if you wanted over 100 lines, or even 1000 thousands!! This would take lots of lines and code and actually can be done in one line of code. We use the WHILE command. Here is how it works....
There are three types of loops, the while loop, the do-while loop, and the for loop.
To get started, we are going to focus on the while loop for a little while. For one thing, the only loop you really need is while .
Just as with conditional ( if / else ) structures, a while loop employs a boolean test condition. If the test evaluates to true, the instructions enclosed in curly brackets are executed; if it is false, we continue on to the next line of code. Th e difference here is that the instructions inside the while block continue to be executed over and over again until the test condition becomes false.
Now looking at the above code we can think of it as....
1) Draw a line
2) Draw the next line 10 pixels to the left
3) Draw until you have drawn to the x-value of 150.
Here is how the code looks...
To get started, we are going to focus on the while loop for a little while. For one thing, the only loop you really need is while .
Just as with conditional ( if / else ) structures, a while loop employs a boolean test condition. If the test evaluates to true, the instructions enclosed in curly brackets are executed; if it is false, we continue on to the next line of code. Th e difference here is that the instructions inside the while block continue to be executed over and over again until the test condition becomes false.
Now looking at the above code we can think of it as....
1) Draw a line
2) Draw the next line 10 pixels to the left
3) Draw until you have drawn to the x-value of 150.
Here is how the code looks...
There is a dark disease that may enter your code..THE INFINITE LOOP!!! AWWW SCARY!!! This loop will freeze your computer and screen. For example never run the code...
int x = 0;
while (x < 10) {
println(x);
x = x – 1;
}
As you can see that x will NEVER be less than 10 and will run forever. If you disregarded my warning and ran it, you will have to force stop your program...shame on you for not listening.
Another loop is the FOR
This is a loop that will increment a variable every time it loops until it reaches a certain value. A picture is shown below.
int x = 0;
while (x < 10) {
println(x);
x = x – 1;
}
As you can see that x will NEVER be less than 10 and will run forever. If you disregarded my warning and ran it, you will have to force stop your program...shame on you for not listening.
Another loop is the FOR
This is a loop that will increment a variable every time it loops until it reaches a certain value. A picture is shown below.
In the example you see
for( int i=0;, i<10; i++{
}
This means that this code will create a LOCAL variable called i and set it to 0. It will run as long as i is less than 10, and every time it runs it increases i by one.
Some shortcuts are:
for( int i=0;, i<10; i++{
}
This means that this code will create a LOCAL variable called i and set it to 0. It will run as long as i is less than 10, and every time it runs it increases i by one.
Some shortcuts are:
- i++ means i=i+1;
- i-- means i=i-1;
- x+=2 means x=x+2;
- x*=3 means x=x*3;
TASK
Create the following pictures using the FOR command.
Create the following pictures using the FOR command.
Local vs Global Variables
In the last section I mentioned a LOCAL variable...what the heck is that? Well...up to now I we have always declared our variables at the start of the code. This makes them global, which means any part of the code has access to them. While the variable in the FOR command is local, which means only the code in the FOR command has access to it.
If you create a variable inside a function, then ONLY the code in the function has access to it. Also any code above where the variable is declared, in the same function, will NOT have access to it. For example......
void setup() {
size(200,200);
}
void draw() {
background(0);
int x = 0;
while (x < width) {
stroke(255);
line(x,0,x,height);
x + = 5;
}
}
void mousePressed() {
println( "The mouse was pressed! ");
}
Only the lines under int x=0; which are inside the draw command have access to the variable x...meaning these lines only.
while (x < width) {
stroke(255);
line(x,0,x,height);
x + = 5;
Also, it is a general rule not to declare the same variable globally and locally, as the local variable will overwrite the global variable. Using loops, inside loops, allows for some interactivity...Run the following code and see how it works...
void setup() {
size(255,255);
background(0);
}
void draw() {
background(0);
// Start with i as 0
int i = 0;
// While i is less than the width of the window
while (i < width) {
noStroke();
float distance = abs(mouseX - i);
fill(distance);
rect(i,0,10,height);
// Increase i by 10
i += 10;
}
}
In the last section I mentioned a LOCAL variable...what the heck is that? Well...up to now I we have always declared our variables at the start of the code. This makes them global, which means any part of the code has access to them. While the variable in the FOR command is local, which means only the code in the FOR command has access to it.
If you create a variable inside a function, then ONLY the code in the function has access to it. Also any code above where the variable is declared, in the same function, will NOT have access to it. For example......
void setup() {
size(200,200);
}
void draw() {
background(0);
int x = 0;
while (x < width) {
stroke(255);
line(x,0,x,height);
x + = 5;
}
}
void mousePressed() {
println( "The mouse was pressed! ");
}
Only the lines under int x=0; which are inside the draw command have access to the variable x...meaning these lines only.
while (x < width) {
stroke(255);
line(x,0,x,height);
x + = 5;
Also, it is a general rule not to declare the same variable globally and locally, as the local variable will overwrite the global variable. Using loops, inside loops, allows for some interactivity...Run the following code and see how it works...
void setup() {
size(255,255);
background(0);
}
void draw() {
background(0);
// Start with i as 0
int i = 0;
// While i is less than the width of the window
while (i < width) {
noStroke();
float distance = abs(mouseX - i);
fill(distance);
rect(i,0,10,height);
// Increase i by 10
i += 10;
}
}
TASK
Create a grid of squares (each colored randomly) using a for loop. (Hint: You will need two for loops!) Recode the same pattern using a “ while ” loop instead of “ for. ”
Create a grid of squares (each colored randomly) using a for loop. (Hint: You will need two for loops!) Recode the same pattern using a “ while ” loop instead of “ for. ”
BIG TASK ---MIGHT TAKE A WHILE...
Step 1. Create a creature, using variables instead of hard numbers.
Step 2. Write a series of assignment operations that alter the values of those variables and make the design dynamic. You might also use system variables, such as width , height, mouseX , and mouseY .
Step 3. Using conditional statements, alter the behavior of your design based on certain conditions. What happens if it touches the edge of the screen, or if it grows to a certain size? What happens if you move the mouse over elements in your design?
Step 1. Create a creature, using variables instead of hard numbers.
Step 2. Write a series of assignment operations that alter the values of those variables and make the design dynamic. You might also use system variables, such as width , height, mouseX , and mouseY .
Step 3. Using conditional statements, alter the behavior of your design based on certain conditions. What happens if it touches the edge of the screen, or if it grows to a certain size? What happens if you move the mouse over elements in your design?