Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

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

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

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

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

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

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

7 Arrays Sometimes we have multiple variables similar in type and nature and we struggle to name them ourVar1, ourVar2 … Wouldn't it be great to have a single variable that would hold all of those and consolidate them in an elegant manner.

8 Arrays Arrays are chains of variables that can be addressed by number. All the variables (items) in an array are of the same type (int, float, object) Three steps: –Declaring –Assigning values –Using

9 Arrays - Declaring Creating a variable to hold the array, naming it and defining the data type and number of items. Syntax : data type [] name = new [ number of items]; any data type can be used number of items cannot be changed easily and must be an int (not float)

10 Arrays - assigning You cannot assign values to an array, just to an item in an array: Syntax : ourArray[index] = new value ourArray[0] = 5; Numbers start from 0 careful not exceed the number of items in the array

11 Arrays - using You can use an array anywhere a variable can be used : Syntax : ourArray[index] x = ourArray[4]*3; strokeWeight(ourArray[4]); if (ourArray[x] > 16) {

12 Arrays int x=10; float y=10; int[] ourArray = new int[ x]; ourArray = 7; Is This OK?

13 Arrays int x=10; float y=10; int[] ourArray = new int[y]; ourArray[5] = 7; Is This OK?

14 Arrays int x=10; float y=10; int[] ourArray = new int[x]; ourArray[10] = 7; Is This OK?

15 Functions are cool, they let us organize our code into smaller units But we still need to handle all the data in a central location (global variables) When you do stuff that has multiples, it becomes cumbersome to manage all the variables. Objects

16 objects For example if we had a program with 3 bouncing balls we would need variables to remember position X and Y, color R, G and B and speed all times 3 = 18 variables. Wouldn't it be nice to have an entity that consolidates all the data we need for each ball? then we would only need 3 of these “meta” variables (one for each ball). Wouldn't it be nice if that entity could also hold actions (functions) so we could say something like “ball one, move to your new position”.

17 Objects Just like function allowed us to consolidate multiple statements and name them, the object allows us to consolidate functions and data and encapsulate them. Using objects is very easy and intuitive, defining your own objects can sometimes require a fresh look at the structure of your code.

18 Objects There are 3 steps in the life cycle of an object: –Declaring –Initializing –Using Let’s assume someone has created an object called “ball” that knows how to move and bounce:

19 Objects - declaring Declaring. This means creating a variable to hold the object. It is global and goes on the top. Remember variable declaration ? float myColor; Objects are declared the same, but we put the object’s (class) name as the type: ball myBall;

20 Objects - initializing Initializing - this means giving birth to the object and also setting up any initial settings by means of parameters. This takes the form of: variable name = new class name(parameters) So, for our ball example, we probably have to set the initial location of the ball: ourBall = new ball(x,y);

21 Objects - using Using - this means asking the object to perform the functions that it was designed to undertake. This takes the form of : variable name. function name ( parameters); So, for our ball example, it probably has a function to change the color of the ball : ourBall.changeColor(255);

22 Objects creating/defining classes In the case of our ball example, it is conceivable that we would like to have more than one ball, so we would declare multiple objects or multiple instances of the same “definition” This definition, or set of functions and data that will makeup the object is called “class” Some people find the cookie cutter analogy helpful, you make one cookie cutter (class) and it can create multiple cookies (objects)

23 Objects - defining classes The main challenge in objects is to define the class. This has 4 parts: –The class name –The data (variables) –A constructor –Functions

24 Objects - class name Every class needs a name, that will be used as the data type for variables that store it. Remember? ball myBall; The syntax for naming your class is : class name { the class definition statements } No parentheses and no void

25 Objects - data Most classes have variables declared in them to store information about the object, these variables are local. class ball { int xPosition; int yPosition: … … } If you want to access these variables from another part of your code you can use the dot “.” notation i.e.. myBall.xPosition

26 Objects - constructor All classes have one special function that is automatically called when the object is initialized with “new” It always has the name of the class. It is declared like any other function but without the void, because it cannot return anything. class ball { int xPosition; int yPosition: ball(){ xPosition = width/2; yPosition = height/2; } … } You can pass parameters like a normal function and they will be passed by “new”

27 Objects - functions Classes can have many functions with many names, they are similar to normal functions. class ball { int xPosition; int yPosition: ball(){ xPosition = width/2; yPosition = height/2; } void move(){ xPosition = xPosition+1; } You can pass parameters like a normal function and the syntax to passing them would be : ourBall.changeColor(255);


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

Similar presentations


Ads by Google