Lesson 2 // the last chapter
- maiguzman28
- May 7, 2020
- 5 min read
Updated: Jul 16, 2020
Since the second lesson was so much input, I decided to make a separate Post about the last chapter of this lesson: Loop.

I am liking this, because with looping you need to write a lot less comands. I had the problem, that for one excercise I did not know where the problem was, and since the code was very long, it took a while to figure out where the error was.
There are three types of loops, the while loop, the do-while loop, and the for loop. After a quick explanation from the book, here comes the first excercise:
Exercise 6-1: Fill in the blanks in the code to recreate the following screenshots.
The first one I tried to change a little thing to see what happens and then understand it better. And when I changed it back, then I understood. The second one was a lot more difficult, I tried a lot of combinations but at the end I did it!



The next topic is FOR Loop. I found this table very important to understand:

I need to include the most relevant information from the book here, it helps me to understand better.
• Initialization —Here, a variable is declared and initialized for use within the body of the loop. This variable is most often used inside the loop as a counter. • Boolean Test —This is exactly the same as the boolean tests found in conditional statements and while loops. It can be any expression that evaluates to true or false. • Iteration Expression —The last element is an instruction that you want to happen with each loop cycle. Note that the instruction is executed at the end of each cycle through the loop. (You can have multiple iteration expressions, as well as variable initializations, but for the sake of simplicity we will not worry about this now.)
This references are also very important, and I would like to find them easily on my blog:

I decided to take a break after finishing reading the Lesson. I really did not feel like I understood everything and was feeling very frustrated. We had on friday a Zoom call with Tim Rodenbröker, that was very inspiring.
So I decided to change my defeated attitude, since it was not helping at aaall. With a glass of wine I decided to chill and watch a marathon of the YouTube tutorials of the whole lesson. I am so glad I did, it was very time consuming but I filled in the blanks that way. I decided to write in my blog the most importan things I relearned watching the videos.
Chapter 4: Variables
It is best to use float as variable as int, because is more flexible! Random: If it has only one value, its the maximum. If it has two then thats the minimum and maximum The random function gives you random values and gives you a floating point value like 95.837466. Tip: if you only write the background once in setup instead of draw, the objects appear and multiply themselves instead of moving once.
Chapter 5: Conditionals
Boolean expression: Only true or false You need to have () in the whole boolean expression! Otherwise it doesn’t work. AND OR NOT
Chapter 6: Loop
Some variables exist (i.e., are accessible) throughout the entire course of a program’s life— global variables —and some live temporarily, only for the brief moment when their value is required for an instruction or calculation— local variables. In Processing, global variables are declared at the top of the program, outside of both setup( ) and draw( ). These variables can be used in any line of code anywhere in the program. This is the easiest way to use a variable since you do not have to remember when you can and cannot use that variable. You can always use that variable (and this is why we started with global variables only).
While Loops: Repeat this code a bunch of times Loops must always have an exit condition! StrokeWeight is to determine the stroke If we want to for example draw a line continuously for the width of the screen: Float x = 0; Void setup () { Size(400,300); } Void draw (){ background(0); Stroke(255); strokeWeight(2); x = 50; While (x < width) { line(x, 0 x, height); x = x + 50; } } To implement y: Float x = 0; Float y = 0; Void setup () { Size(400,300); } Void draw (){ background(0); Stroke(255); strokeWeight(2); x = 50; While (x < width) { line(x, 0, x, height); x = x + 50; } Y = 50; While (y < height) { line(0, y, width, y); y = y + 50; } } To get more flexible: we can implement spacing as a variable! And as a variable we can make it random! Float x = 0; Float y = 0; Void setup () { Size(400,300); } Void draw (){ background(0); Spacing = spacing + random (-2,2); Stroke(255); strokeWeight(2); x = 0; While (x < width) { line(x, 0, x, height); x = x + spacing; } Y = 0; While (y < height) { line(0, y, width, y); y = y + spacing; } } _________________
For Loop It is not adding functionality, we are just learning a short hand way to write a while loop. Int x = 0; While (x = < width) { line (x,0,x,height); x = x + 20; } Is exactly the same as: For (int x = 0; x < width; x = x + 20)} Line (x, 0, x, height); } The for Loop is a very specific format, which gives you three options, the while loop is more flexible, you can have more coding options! _________________
Variable Scoop
What happens if we put the variables not on the top of the page, but for example in setup? Or draw? Then the variable is only usable in THAT block of code, meaning draw or setup.
This is very important when you have a lot of variables in the code. If we do not need them all all the time, then makes it sense not to declare them global, but to put them in the code.
If we want to animate and for example let a line appear one by one this is a good code for that:
Float endX = 0;
Void setup(){
size(400, 300);
}
Void draw(){
background(0);
strokeweight(2);
stroke(255);
Int x = 0;
While (x < endX) {
line(x, 0, x height);
x = x +20;
}
endX = endX + 1:
}
_________________
Nested Loop
For every raw, draw a column! This its a loop within a loop.
So we can solve this by doing two for loops!
This pretty much says: for every raw draw a rectangle and loop it the whole width.
size(400, 300);
background(0);
strokeweight(2);
stroke(255);
fill(127);
For (int y = 0; y < height; y = y + 20) {
for (int x = 0; x < width; x = x + 20( {
rect(x, y, 20, 20);
}
I was able to do the next Excercise after watching the videos:
Exercise 6-8: 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. ”
size(300, 300);
background(0);
strokeWeight(2);
stroke(255);
for (int y = 0; y < height; y = y + 20) {
for (int x = 0; x < width; x = x + 20) {
fill(random(255));
rect(x, y, 20, 20);
}
}

Comentários