Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.

Similar presentations


Presentation on theme: " Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in."— Presentation transcript:

1

2  Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in variables  Using random values for variables Learning Processing: Slides by Don Smith2

3  The computer has memory  The computer uses that memory to remember data it needs  A variable is a pointer to a location in the computer’s memory where that data is stored  Variables allow a programmer to save information from one point in the program and refer back to it at a later time  Variables can keep track of information related to shapes, color, size, location  The power of a variable is not that it remembers a value, but that those values can vary and we can alter them.  The value of the variable can change overtime Learning Processing: Slides by Don Smith3

4  Variable Analogies:  Different sizes ▪ Like a bucket ▪ Like a locker ▪ Like a post-it note ▪ Like a shoe box ▪ Like a dresser drawer  Graph Paper/Spreadsheet ▪ Many ‘storage’ places, all with unique locations (x,y) ▪ In Excel, E3 could be named ‘Billy’s Score’ ▪ Billy’s score can change! 4 A B C D E F 1234512345

5  Consider this analogy 5 Billy’s score 5 20 53 83 101 Jane’s Score 10 25 47 68 85 In a game of Scrabble:  The paper is the “computer memory”  Billy’s score and Jane’s score are the names of variables  The score is a specific Type of data  The score value changes as the game progresses  three ingredients for a variable  Data Type  Name  Value

6  In Algebra:  x = y * z  Named with single letters to represent some number  In Programming:  We use longer, more descriptive names  Variables refer to ‘memory locations’  Stored in ‘RAM’ ▪ Random Access Memory  Have a ‘size’ ▪ How many bytes of memory ‘wide’ Learning Processing: Slides by Don Smith6

7  Variables can hold primitive values or references to objects and arrays. For now we will just focus on primitive values  Primitive values are the building blocks of data on the computer and typically involve a singular piece of information, like a number or character.  Before you can use a variable in a program it has to be declared. By declaring the variable you tell the computer the kind of data the variable will contain. This is called the Data type  Give the variable a Unique name.  You can also assign the variable a value, but you do not have to at the time of the declaration. Learning Processing: Slides by Don Smith7

8 What is a Data Type?  Each type requires a specific amount of storage and the type declaration lets the computer know how much storage to reserve for that variable  ‘Primitive’ types include three main categories ▪ Integers (int)– Whole Numbers (positive and negative), no fractions ▪ Floating point (float) – Numbers with fractional parts and exponents ▪ Characters (char)– One letter that you can type : inside of single quotes ▪ ‘ A’, ‘ B’, ‘C’, ‘a’, ‘b’, ‘1’, ‘2’, ‘3’, ‘%’,’‘&’…. Learning Processing: Slides by Don Smith8

9  Integer Types  byte: A very small number (-127 to +128)  short: A small number (-3,2768 to +3,2767)  int: A big number (-2,147,483,648 to +2,147,483,647)  long: A really huge number  Floating Point Types  float: A decimal number such as 3.14159  double: A huge decimal number used for advanced mathematics  Other Types  boolean: true or false (0 or 1)  char: One symbol in single quotes ‘a’ 9

10  Integer Types  byte: 123  short: 1984  int: 1784523  long: 234345456789  Floating Point Types  float: 4.0  double: 3.14159  Other Types  boolean: true  char: ‘a’ Learning Processing: Slides by Don Smith10

11  How do you decide if you need a numeric or a character type?  If you plan on doing ‘math’ on the variable, then you MUST use a numeric type ▪ What is the letter ‘a’ times the letter ‘c’? ▪ Notice the single quotes around characters  If you plan on using “Strings” (later), they are just words made up of characters. ▪ “Bob” and “Mary” are Strings (of characters) ▪ Notice the double quotes around strings ▪ What is the string “Bob” times the string “Mary”? Learning Processing: Slides by Don Smith11

12  What is in a name  Variables are declared by…  first defining the data type: int (integers), float (floats), char (characters), etc  Second giving the variable a name: ▪ Must be one word (no spaces) ▪ Must start with a letter (not a number) numbers can be used in the name ▪ Must not contain punctuation or special characters ▪ Underscore is allowed in name ▪ it needs to make sense and be descriptive ▪ Example: int count; data type is Integer, the variable name is count  End the declaration with the semi colon. Learning Processing: Slides by Don Smith12

13  There are some ‘rules’ and some ‘best practices’  Rules  Letters, Digits and underscore ( _ ) are OK to use  Cannot start with a digit ( 0,1,…9 )  Cannot use reserved words ▪ mouseX, int, size..  Best Practices  Use descriptive names ▪ boolean moreToDo ;  Use ‘camelHump’ notation ▪ Start with lower case ▪ Each new word starts with Upper Case Learning Processing: Slides by Don Smith13

14  Setting an initial value into the contents of the variable ▪ Pseudocode: Set NumPlayers to 5  Can be done in two ways:  During Declaration: On one line ▪ int count = 50; // declare and initialize  After declaration: On two lines ▪ int count; // declare the variable ▪ count = 50; // initialize the value  Can also be initialized with a calculation! ▪ int max = 100; ▪ int min = 10; ▪ int count = max – min; // calculation Learning Processing: Slides by Don Smith14

15 Type name (optional initialization) ; int count = 0; // Declare an int, initialized to 0 char letter = 'a'; // Declare a char, initialized to 'a' double d = 132.32; // Declare a double, initialized to 132.32 boolean happy = false; // Declare a boolean, initialized to false float x = 4.0; // Declare a float, initialized to 4.0 float y; // Declare a float (no assignment) float z = x * y + 15.0; // Declare a float, initialize it to // x times y plus 15.0. After declaration Assignments: count = 1; letter = ‘b’; happy = true; y = x + 5.2; // Assign the value of x plus 5.2 Learning Processing: Slides by Don Smith15

16  You can only initialize a variable to a value of the same, or compatible type.  Which initializations are compatible?  int count = ‘a’;  char letter = 0;  double deposit = “Fred”;  boolean happy = 1;  float feet = 6;  int inches = feet * 12;  long giant = feet * 3.0; Learning Processing: Slides by Don Smith16

17  If you forget to assign a value to a variable Processing will assign a default value of…  Int = 0  float = 0.0 Its good to always initialize a value to avoid confusion. Learning Processing: Slides by Don Smith17

18  Remember that your code is in ‘blocks’  Variables can go inside or outside these blocks  For now, we will put them ‘outside’ (before) blocks

19  What variables would be required to play? Learning Processing: Slides by Don Smith19

20  Remember that processing calls draw() in a loop  If you want the variable to change every time:  Declare and initialize it outside of draw()!  Change it inside draw()! Moves as circleX increases 

21 Learning Processing: Slides by Don Smith21 circleX Is initialized to 0 Used first time draw() is called ellipse(circleX, circleY, 50,50); Then circleX = circleX + 1; Next time draw() is called, circleX is 1 ellipse(circleX, circleY, 50,50); Then circleX = circleX + 1; Next time draw() is called, circleX is 2 ellipse(circleX, circleY, 50,50); Then circleX = circleX + 1;.. Until circleX is over 200, and the circle just moves off the right side of the screen, never to be seen again! It is often useful to make a ‘table’ of all of the variables that are being changed every time through a ‘loop’ Call to draw() circleX 10 21 32 43 54 … 200199

22 Make things more interesting using more variables!  Declare and initialize them outside of draw()!  Change them inside draw()! Call to draw() circleXcircleYcircleWcircleH 10050100 20.5 50.5100.5 31.0 51101 104.5 54.5104.5 3014.5 64.5114.5 10049.5 99.5149.5 20099.5 149.5199.5

23  Processing provides many ‘built-in’ variables:  These are not for you to play with, but may change!  mouseX, mouseY, pmouseX and pmouseY  width: Width (in pixels) of sketch window  height : Height (in pixels) of sketch window  frameCount : Number of frames processed  frameRate : Rate (per sec.) that frames are processed  key : Most recent key pressed on keyboard  keyCode : Numeric code for which key pressed  mousePressed : True or false (pressed or not?)  mouseButton : Which button (left, right, center) Learning Processing: Slides by Don Smith23

24  Processing (and all programming languages) provide a way to get a random value when your program runs.  random() is a ‘function’ that returns a float  You can assign this number to a variable and use it!  Some examples: Learning Processing: Slides by Don Smith24

25  (int)  Since random() returns a floating point number and w is an int, we need to change the type from float to int.  This is called ‘casting’  random(1,100) ;  (1,100) are ‘parameters’ which tell the random function the range of numbers to return ▪ w will be between 1 and 99 (not quite 100) Learning Processing: Slides by Don Smith25

26  Use variables and random() to make a Zoog or your character move from upper left to lower right of the screen.  Plan!  Declare variables  Use variables  Use Random Learning Processing: Slides by Don Smith26

27  Variables have names and types  There are rules and best practices for naming variables  You must declare your variables  Determines the ‘type’, size of storage, and maximum values  You can initialize variables in different ways  Variables have many uses to programmers  processing provides ‘system’ (built-in) variables  You can use random numbers to make your program run differently every time Learning Processing: Slides by Don Smith27


Download ppt " Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in."

Similar presentations


Ads by Google