Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 ICM Week 2

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); }

3 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);

4 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)

5 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

6 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

7 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

8 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 variable ie - x = 7+5

9 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”

10 Variables - our variables Three stages : –Declare - giving it a name –Assign value - storing a value in it –Using it - doing something with the value

11 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 !@#$%^&*.,

12 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();

13 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);

14 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

15 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

16 Conditional statements - if if(expression) { } assesses anything in the parentheses and if true, executes block of code that follows. if (x==6){ strokeWeight(x); }

17 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); }

18 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); }

19 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)

20 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)

21 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)

22 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...

23 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.

24 Loops - for Look at the while loop, we can point out 3 elements that are usually present: void draw(){ int i=0; ->Initialization while (i Boolean test println(i); i=i+1;->Iteration expression }

25 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) {

26 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); } }


Download ppt "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."

Similar presentations


Ads by Google