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

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Lesson One: The Beginning Chapter 3: Interaction Learning Processing: by Daniel Shiffman Presentation by Donald W. Smith Graphics from text.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Variables and Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Input & Output: Console
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Datatypes, Variables, Constants Flash Class. What Does ActionScript Do? Automates Examples: – Tells animation what to do button is clicked – Turn off.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Copyright Curt Hill Variables What are they? Why do we need them?
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of.
Lesson Two: Everything You Need to Know
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Chapter 2. Variable and Data type
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Week 2 - Wednesday CS 121.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Basic Computation
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Basic Elements of C++.
Multiple variables can be created in one declaration
Variables and Primative Types
Lesson Two: Everything You Need to Know
Variables ,Data Types and Constants
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Variables ICS2O.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Variables Here we go.
Primitive Types and Expressions
Variables and Operators
Variables and Constants
Presentation transcript:

 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

 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

 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

 Consider this analogy 5 Billy’s score Jane’s Score 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

 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

 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

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

 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  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

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

 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

 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

 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

 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

Type name (optional initialization) ; int count = 0; // Declare an int, initialized to 0 char letter = 'a'; // Declare a char, initialized to 'a' double d = ; // Declare a double, initialized to 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 ; // Declare a float, initialize it to // x times y plus 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

 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

 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

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

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

 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 

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 …

Make things more interesting using more variables!  Declare and initialize them outside of draw()!  Change them inside draw()! Call to draw() circleXcircleYcircleWcircleH

 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

 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

 (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

 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

 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