Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4, Variables Brief Notes

Similar presentations


Presentation on theme: "Chapter 4, Variables Brief Notes"— Presentation transcript:

1 Chapter 4, Variables Brief Notes
Purpose of Chapter: Continue to program more efficiently by using variables. Allowing for information to vary creates more flexibility.

2 Definition Variable: A value that can change, depending on conditions or on information passed into the program. What are other synonyms or ways to describe variable? What are some variables that you might need for a game of pong?

3 Types of Variables These are the common types
int: Whole numbers – positive and negative; i.e. a large number -2,147,483,648 to + 2,147,483,647) float: A number with decimal places, such as char: A character such as “a” or “b” boolean: True or False See page 52 for others

4 Rules for naming variables
Best Practices One word, no spaces Cannot start with number No punctuation or special character except “_” Must be unique Names should be meaningful. Avoid words that appear elsewhere in processing, such as width, height, key, mouseX, etc. camelCase is a good idea

5 Declare and initialize variables
You can declare a variable by stating the type and the name: int starter; Or you can declare and initialize at the same time: int starter = 1; Variables can be initialized by another variable or by a mathematical expression. float counter = x+10 Question: In general, when should you use a variable?

6 In this exercise, two variables are used for the X & Y positions
In this exercise, two variables are used for the X & Y positions. Then each time the circle is drawn onto the screen, the X position increases by one. Declare and initialize two integer variables at top of code. int circleX = 0; int circleY = 100; void setup() { size(200,200); } void draw() { background(255); stroke(0); fill(175); ellipse(circleX,circleY, 50,50); circleX = circleX + 1; Use the variables to specify the location of an ellipse

7 This is what is happening in the program:

8 Random Random is a math function that returns an unexpected value within a specified range. Syntax: random(high) random(low, high) Random returns floating point numbers. To convert to an integer, enclose it in the int() function: int w = int(random(low, high));

9 Translate The translate() function specifies a horizontal and vertical offset for shapes. However, we will cover it in Chapter 14 where more meaningful examples can be applied.

10 In-Class Exercise (ICE)
Pair up with a classmate. Create an object that moves off the screen. Not too complex because you will vary the horizontal and/or vertical variable so that it moves. Examples: car, balloon, motorbike, airplane, boat, or even two different vehicles. As you start coding, add both names by using comments. Be sure to create a background or platform to improve visual interest (grass, pavement, etc.) Complete your design Grading will be Pass/Fail, and will be worth 1% of final grade On Monday, one team member will upload to openprocessing.org For now, each student will start a free account at and browse some pages.


Download ppt "Chapter 4, Variables Brief Notes"

Similar presentations


Ads by Google