Week. 5

REVIEW if() – code example
A. Repetition(LOOP)
As you write more programs, you’ll notice that patterns occur when lines of code are repeated, but with slight variations. A code structure called a ‘for’ loop makes it possible to run a line of code more than once to condense this type of repetition into fewer lines. This makes your programs more modular and easier to change.
1. 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. The difference here is that the instructions inside the while  block continue to be executed over and over again until the test condition becomes false.int x = 0;
int y = 0;
int spacing = 20;
void setup(){
size (400,400);
}
void draw(){
while(x<width){
line(x,y,x,height);
x = x +spacing;
}
}

2. FOR
The for loop is a nifty shortcut for commonly occurring while  loops. Before we get into the details, let’s talk through some common loops you might write in Processing  and how they are written as a for loop.


• 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
CODE EXAMPLE : for()

B. Local vs. Global Variables (AKA “ Variable Scope ” )

Up until this moment, any time that we have used a variable, we have declared it at the top of our program above setup( ).At this point, we need to know the concept of variables’ scope

Global variables,which exist (i.e., are accessible) throughout the entire course of a program’s life, Local Variables which live temporarily, only for the brief moment when their value is required for an instruction or calculation. 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.

Local variables are variables declared within a block of code. So far, we have seen many different examples of blocks of code: setup( ) , draw( ),  mousePressed( )  , and keyPressed( ), if statements,and while and for loops…. A local variable declared within a block of code is only available for use inside that specific block of code where it was declared.

** the best example of Local Variable : in FOR
A for loop offers up a spot for a local variable within the “ initialization ” part:
for (int i = 0; i < 100; i + = 10) { // valid only inside of “( )”
stroke(255);
fill(i);
rect(i,0,10,height);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s