23
Variables and Operators

Variables and Operators

  • Upload
    arne

  • View
    51

  • Download
    0

Embed Size (px)

DESCRIPTION

Variables and Operators. What is a Variable? Variables allow a program to store data at one point and refer back to it later A variable is container for data with an associated type and name, or identifier. Type. - PowerPoint PPT Presentation

Citation preview

Page 1: Variables and Operators

Variables and Operators

Page 2: Variables and Operators

What is a Variable?

• Variables allow a program to store data at one point and refer back to it later

• A variable is container for data with an associated type and name, or identifier

Page 3: Variables and Operators

Type

• “A number of … things having in common … characteristics that distinguish them as a … class.”– Free Dictionary definition

• For example:– Characters– Strings– Integers– Colors– Any Java class

Page 4: Variables and Operators

Creating a Variable

• Before a variable can be used in a program it must be declared

• Variables are declared by first stating the type of the data to be stored, followed by the variable’s name

int count;• This declares a variable named “count”

that stores an integer (a whole number)

Page 5: Variables and Operators

Variables and Computer Memory

• All data that a program uses must be stored somewhere in the computer’s memory

• Each piece of data is stored in a specific location, referred to as the “address”

• A variable is a name assigned to the address that contains the data

Page 6: Variables and Operators

Data Types

• We can store different types of data• In Java, variables can hold primitive data

types or references to objects• Primitive data types include types for

true/false data, characters, whole numbers and real numbers

• For now, we’re just going to look at primitive data types, we’ll look at objects later

Page 7: Variables and Operators

Primitive Data Types

• boolean — truth values — true or false• char — characters — ’a’, ’G’, ’#’, ’3’• Integer values – whole numbers

– byte (-128 to 127)– short (-32768 to 32767)– int (-2147483648 to 2147483647)– long (-9223372036854775808 to 9223372036854775807)

• Floating values -- decimals– float (1.401298e-45 to 3.402823e+38)– double (4.94065645841246e-324 to 1.79769313486231e+308)

Page 8: Variables and Operators

Strongly-Typed Languages

• Java is a strongly typed language, we have to define what kind of data we want to hold in a variable before it can be used

• The advantage of a strongly typed language is that we can get the compiler to check that we are using a variable correctly before we run the program

Page 9: Variables and Operators

Weakly Typed Languages

• JavaScript and ActionScript are weakly typed languages, they do not require variables have their data type defined in advance

• The advantage of weakly typed languages is that we can program quickly without having to worry about declaring variables first

Page 10: Variables and Operators

Naming Variables

• There are some restrictions on variable names:– Variable names can only include a limited

range of characters: a-z, A-Z, 0-9, _– Variable names cannot contain spaces, the

underscore character “_” is often used instead of a space

– Variable names must not start with a number

Page 11: Variables and Operators

Variable Naming Conventions

• In Java programs, variable names typically begin with a lowercase letter and use a capital letter to start each new word– e.g. width, rect4, fillColour, isFilled

• Variables that refer to constant data typically use all uppercase letters and underscores between each word– e.g. PI, CENTER, MAX_VALUE

Page 12: Variables and Operators

Initializing Variables

• A variable can be declared with no value and then assigned a value later

// declare an int named y1 but do not initializeint y1;// assign the value 5 to the variable y1y1 = 5;

• A variable can be initialized at the time it is declared

// declare an int named y1 initialized to 5

int y1 = 5;

Page 13: Variables and Operators

Initializing Variables

• A variable can be initialized with a value, the value of another variable,or by evaluating an expression// declare a char named letter initialized to ‘a’char letter = ‘a’;

// declare a double named d1 initialized to 132.32double d1 = 132.32;

// declare a double named d2 initialized to d1double d2 = d1;

// declare a float name z initialized to x * y + 15float z = x*y + 15.0f;

Page 14: Variables and Operators

Arithmetic Operators

= assignment operator x=9+ addition 3 + 4 - subtraction 5 - 7 * multiplication 5 * 5 / division 14 / 7 % modulo 20 % 7

++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1

Compound Assignment Operators += -= *= /= %=

Page 15: Variables and Operators

Integer Operations

+, -, *, /, and %

5 / 2 yields an integer 2.

5.0 / 2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)

Page 16: Variables and Operators

Operator Precedence• Here’s another problem. What’s the answer to this?

x = 7 + 3 * 6;

• Two Options (depending on the order of operations):• Perform addition first:

7 + 3 = 10 10 * 6 = 60• Perform multiplication first:

3*6 =18 7+18 = 25

• Which option is correct?

Page 17: Variables and Operators

Operator Precedence

Operator(s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. If the parentheses are nested, the

expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right.

*, /, % Multiplication, Division Modulus

Evaluated second. If there are several, they are evaluated left to right.

+ , - Addition Subtraction

Evaluated last. If there are several, they are evaluated left to right.

Operator precedence represent rules for evaluating mathematical expressions.

Every programming language has similar rules.

Page 18: Variables and Operators

Built-In Variables

• Processing has some built-in variables that are quite useful. Since they are built-in, these variables should not be declared, initialized or assigned; they should just be read. – width / height :: The dimensions of the

window. – mouseX / mouseY:: The current coordinates of

the mouse. – frameCount :: The number of frames that

have been drawn since the program started.

Page 19: Variables and Operators

Color Models

• colorMode(RGB, 255); // processing’s default color model

// used almost exclusively in // computer science applications

• colorMode(HSB, 360, 100, 100); // hue, saturation, value(brightness) // used predominately in art, available // in most graphics and animation

packages

Page 20: Variables and Operators

Examples// 1 colorful rectangle centered in the canvas

size(300,300);

// colorMode(HSB,255,255,255);

background(20,20,255);

fill(20,255,20);

stroke(255,20,20);

rectMode(CENTER);

/*

CENTER is a built-in variable

that can’t be changed—a constant

*/

rect(width/2,height/2,50,50);

Page 21: Variables and Operators

More Examples

// 5 small rectangles across

// the top of the canvas

size(300,300);

int xIncrement=width/7;

rect(xIncrement, 10, 20, 20);

rect(xIncrement*2, 10, 20, 20);

rect(xIncrement*3, 10, 20, 20);

rect(xIncrement*4, 10, 20, 20);

rect(xIncrement*5, 10, 20, 20);

Page 22: Variables and Operators

More Examples// the mystery program that changes the display as the size changes

size(950,800);int boxSize = width/3 ;int xPos ;

xPos=boxSize*0 ;fill(xPos*17%255, xPos*11%255, xPos*4%255);// note we are in rectMode(CORNER) by defaultrect(xPos, 0, boxSize, height) ;

xPos=boxSize*1 ;fill(xPos*17%255, xPos*11%255, xPos*4%255);rect(xPos, 0, boxSize, height) ;

xPos=boxSize*2 ;fill(xPos*17%255, xPos*11%255, xPos*4%255);rect(xPos, 0, boxSize, height) ;

Page 23: Variables and Operators

In-class Lab

• Create a pattern of six objects of the same shape and color (such as lines or rectangles).

• Revise your code to set the positions of the shapes with functions of the width and height variables.

• Test your code with several different window sizes to make sure the pattern remains consistent.

• Keeping a monochromatic color palette using the HSB color model, modify your code so that each of your shapes is a different brightness while the hue and saturation remain constant.

• Use a variable to set hue and saturation and test with a several values for each.