icm week 2. structure - statements and blocks of code any single statement ends with semicolon ;...

26
ICM Week 2

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

ICM Week 2

Structure - statements and blocks of code

• Any single statement ends with semicolon ;

• When we want to bunch a few statements together we use curly brackets { } and it is called a block of code. (We use this for functions, loops, conditionals )

{rect(100,10,20,20);line(0,0,30,30);

}

Structure - built-in callback functions

• void setup() { our code …} called once in the beginning of our program

• void draw() { our code… } called repeatedly about 30 times per second or as fast as we ask in frameRate( FPS);

Structure - event driven callback functions

• void mousePressed(){ our code …}Is called and executed whenever the mouse is pressed (Executed once only)

• void keyPressed(){ our code …}Is called and executed whenever any key is pressed (Executed once only)

Structure - continuous event driven callback functions

• void mouseDragged(){ our code …}Is called and executed repeatedly as long as the mouse is pressed and moved (dragged)

• void mouseMoved(){ our code …}Is called and executed repeatedly as long as the mouse is moved

Variables - system variables

• Are not declared, or named by us and we do not control their contents, but we can use the values that they store

• mouseX - the X position of the mouse

• mouseY - the Y position of the mouse

Variables - system variables

• pmouseX - the previous position of mouseX

• pmouseY - the previous position of mouseY

• width - the width of the window from size()

• height - the height of the window from size()

• key - the last key that was pressed

• mousePressed - is the mousepressed (1,0)

• and more - check reference

Variables - What are they

• A variable is a container to hold information in our program, usually numbers or text characters.

• A variable can hold only a certain type of info, so you cannot ask for a text variable to hold numbers, or vise versa.

• To a certain extent, a variable in programming is similar to your high school math formula variableie - x = 7+5

Variables - PEZ dispenser analogy

What it holds

Data type / container type

Variable name (Name of this particular one)

Interacting with it

Irrelevant questions/use

PEZ candy

PEZ dispenser

Snoopy

Put 7 candies into Snoopy,How many candies in Snoopy ?

How many marbles in Snoopy?

Numbers

int

X

X= 12

X = “serious”

Variables - our variables

• Three stages :– Declare - giving it a name– Assign value - storing a value in it– Using it - doing something with the value

Variables - declaring• Declaring - the format is :

data type name ; int x;

• Data types can be:– float - a decimal number (negative or positive)

– int - a whole number (negative or positive)

– char - a character for text

• Name can be letters and number, no !@#$%^&*.,

Variables - assigning value

• Assigning - the format is name = value ;

• value can be a number x=2;• value can be another variable x=y;• value can be a math operation x=y+2;• value can include the initial value of the variable

x=x+1• value can be the result of a function x=random();

Variables - using

• Wherever a number can be used you can use a variable.

• To assign a value to another variable x=y;

• As argument to a function strokeWeight(x);

• As argument with math strokeWeight(x/5);

Conditional statements - Boolean

• Boolean divides the world to true / false which in computers is also 1 / 0

• allows us to evaluate the relationship between values

> greater than (6>5) true< smaller than (6<5) false>= greater or equal (6>=6) true<= smaller or equal (6<=6) true== equals (6==5) false != not equal (6 != 6) false

Conditional statements - logical

! not. The opposite of whatever is on its right! (6==6) false

|| or. At least one expression must be true(6==6 || 6==5) true

&& and. Both expressions must be true (6==6 && 6==5) false

Conditional statements - if

• if(expression) { } assesses anything in the parentheses and if true, executes block of code that follows .

if (x==6){strokeWeight(x);

}

Conditional statements - if / else

• if(expression) { …} else { …}assesses anything in the parentheses and if true, executes the first block of code, if false executes the block that follows the else.

if (x==6){strokeWeight(x);

}else{strokeWeight(x/6);

}

Conditional statements - if / else if

• if(expression) { …} else if (expression) { …}assesses anything in the parentheses, and if true, executes the first block of code, if false, evaluates the “else if” and if true executes the block that follows, and so forth. Placing an “else” in the end will execute if all others failed

if (x==6){strokeWeight(x);

}else if(x>2){strokeWeight(x/6);

} else if(x>4){strokeWeight(x/8);

}

Random numbers

• float random (); is a function that can take 0,1 or 2 arguments and always returns a float x=random(); returns a number between 0-1 (0.032)

x=random(5); returns a number between 0-5 (3.56)

x=random(3,12); returns a number between 3-12 (8.876)

Debugging

• To print a message or the value of a variable use println();

•to print a variable use println(x);

•to print a message use quotes println(“text”)

•You can combine with + println(“The value of x =” + x)

Loops

• We have been using loops since week 2, our void draw(){ } is a loop

• A few drawbacks of draw()– It is endless– There is only one draw()– It updates the screen every time through– It is rather slow (30-60 FPS)

Loops - while

• Syntax :• while (condition) {}

void draw(){int i=0;while (i<5) {

println(i);i=i+1;

}}

Result - 0,1,2,3,4,0,1,2,3,4...

Loops - while

• As long as the condition is true, will continue looping.

• Once the condition is false, will jump to execute the code under the block.

• If the condition is always true, the program will get stuck.

Loops - for

• Look at the while loop, we can point out 3 elements that are usually present:void draw(){

int i=0; -> Initializationwhile (i<5) { -> Boolean test

println(i);i=i+1; -> Iteration expression

}}

Loops - for

• The for loop consolidates the 3 parts of the typical while loop : Initialization, Boolean test, Iteration expression.

• Syntax:for (Initialization ; Boolean test ; Iteration expression){}

for ( int i = 0 ; i< 10 ; i = i+1) {

Loops - for

– The iteration expression (i=i+1) happens in the end of each loop.

– The variable declaration (int i = 0) is local to the loop (no one else knows who i is )

– Multiple loops can be nested to achieve matrix repetition for (int x=0;x<10;x++){

for (int y=0;y<10;y++){point(x,y);

}}