variables, assignment & math storing and naming data

34
Variables, Assignment & Math Storing and naming data

Upload: evangeline-robertson

Post on 28-Dec-2015

232 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Variables, Assignment & Math Storing and naming data

Variables, Assignment & Math

Storing and naming data

Page 2: Variables, Assignment & Math Storing and naming data

Working with data

• Data must be loaded into main memory before it can be manipulated

• Store process:– Allocate memory– Store data in the allocated memory

Page 3: Variables, Assignment & Math Storing and naming data

Declaring Variables

• Variable : memory location whose content may change

• Declaring a variable allocates space and names that space

counter

0101 01100111 01101101 01100101 0110

interestRate

0101 01101111 01001111 01100101 00001101 01100001 01111101 01110101 0110

Page 4: Variables, Assignment & Math Storing and naming data

Bits

• Everything is bits:00000000 00000000 00000000 01100100

• Data Types give meaning to those bits– As integer : 100– As decimal : 6.95x10-308

– As character : 'd'

Page 5: Variables, Assignment & Math Storing and naming data

For now

• Two "main" types– Whole numbersint

– Decimal numbersdouble

Page 6: Variables, Assignment & Math Storing and naming data

Literal Expressions

• Examples of int literals (values):-6728

0

78

+763

• Cannot use a comma within a number:10,000 NO

Page 7: Variables, Assignment & Math Storing and naming data

Literal Expressions

• Examples of double literals (values):12

12.21

-3.1

1.5E12 //1.5 x 1012

1.5E-1 //1.5 x 10-1

Page 8: Variables, Assignment & Math Storing and naming data

Declaring Variables

• Declaring a variable allocates space and names that space– Syntax: dataType identifier;

int counter;

double interestRate;

• No idea what is in allocated memory– Variables start with randomish values!!!

counter

0101 01100111 01101101 01100101 0110

interestRate

0101 01101111 01001111 01100101 00001101 01100001 01111101 01110101 0110

Page 9: Variables, Assignment & Math Storing and naming data

Identifiers

• Identifier : the name given be programmer to something in the program

double diameter; diameter

?? Value ??

Page 10: Variables, Assignment & Math Storing and naming data

Identifiers

• Identifier Rules– Consists of letters, digits, and the underscore

character (_)– Must begin with a letter or underscore

Yes: myNumber my_Number x1

No: 1x my Number my-Number

• C++ is case sensitive – NUMBER is not the same as number

Page 11: Variables, Assignment & Math Storing and naming data

Naming Identifiers

• Identifiers should be self-documenting– Avoid single letters, excessive abbreviationYES: double hourlyPay;

NO: double hp;

• Two main styles:– Capitalizing the beginning of each new word: annualSale

– Inserting an underscore just before a new word: annual_sale

Page 12: Variables, Assignment & Math Storing and naming data

Form and Style

• Possible to use comma to declare multiple variables of same type:

int feet, inch;

double x, y;

• Do not use for unrelated variables:

NO:int miles, age,

daysInSeptember;

Page 13: Variables, Assignment & Math Storing and naming data

• = does not mean "equals"• In C++, = is called the assignment operator

x = 10;

" x gets assigned the value 10""store 10 in location x"

= is not equals

x

10

Page 14: Variables, Assignment & Math Storing and naming data

Declaring & Initializing Variables

• Initializing a variable : assigning a first value to variable

• Can (should) do when declaring :int age = 13;

double x = 12.6;

• Failure to initialize is not a syntax error!!!– Will build and run unpredictably

Page 15: Variables, Assignment & Math Storing and naming data

• Complex assignment statement:

x = 12 + 4;

• Two steps1. Expression on right is evaluated 2. Value is copied to variable on left side

Assignment Statements

X

Page 16: Variables, Assignment & Math Storing and naming data

• Complex assignment statement:

x = 16;

• Two steps1. Expression on right is evaluated 2. Value is copied to variable on left side

Assignment Statements

X

Page 17: Variables, Assignment & Math Storing and naming data

• Complex assignment statement:

x = 16;

• Two steps1. Expression on right is evaluated 2. Value is copied to variable on left side

Assignment Statements

X

16

Page 18: Variables, Assignment & Math Storing and naming data

Using the Value of an Expression

• Variable NOT on left side of = is read from:

int y = 3;int x = y + 5;

y

Page 19: Variables, Assignment & Math Storing and naming data

Using the Value of an Expression

• Variable NOT on left side of = is read from:

int y = 3;int x = y + 5;

y

3

Page 20: Variables, Assignment & Math Storing and naming data

Using the Value of an Expression

• Variable NOT on left side of = is read from:

int y = 3;int x = y + 5;

y

3

Page 21: Variables, Assignment & Math Storing and naming data

Using the Value of an Expression

• Variable NOT on left side of = is read from:

int y = 3;int x = 3 + 5;

y

3

Page 22: Variables, Assignment & Math Storing and naming data

Using the Value of an Expression

• Variable NOT on left side of = is read from:

int y = 3;int x = 8;

y

3

x

8

Page 23: Variables, Assignment & Math Storing and naming data

Using the Value of an Expression

int x = y + 5;"read the value of y, add 5, store the result in x"

cout << x;"read the value of x and print it"

Page 24: Variables, Assignment & Math Storing and naming data

This Makes Sense

x = x + 1;

Page 25: Variables, Assignment & Math Storing and naming data

This Makes Sense

x = x + 1;

x

8

Page 26: Variables, Assignment & Math Storing and naming data

This Makes Sense

x = 8 + 1;

x

8

Page 27: Variables, Assignment & Math Storing and naming data

This Makes Sense

x = 9;

x

9

Page 28: Variables, Assignment & Math Storing and naming data

This Makes Sense

Adds one to whatever is stored in x

"read the current value of x, add 1, store the result back into x"

x = x + 1;

Page 29: Variables, Assignment & Math Storing and naming data

Doing Math

• Math works as normal…

Name Meaning Example Result

+ Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2

Page 30: Variables, Assignment & Math Storing and naming data

Order Matters

• Standard order of operations– PEMDAS

1 + 2 * 3 = 7

(1 + 2) * 3 = 9

5 / 2 + 1.0 2 + 1.0 3.0

5 / (2 + 1.0) 5 / 3.0 1.666667

Page 31: Variables, Assignment & Math Storing and naming data

Division

…except•Dividing integers gives integer answer:

1 / 3 is 0

•Dividing doubles (decimals) gives decimal answer:

1.0 / 3.0 is 0.33333….

Page 32: Variables, Assignment & Math Storing and naming data

Mixed Types

• Integer combined with double makes a double:

1 / 3.0 is 0.3333

1.0 / 3 is 0.3333

Page 33: Variables, Assignment & Math Storing and naming data

Multiplication

…and•No implicit multiplication

NO: 3(2 + 1)

YES: 3 * (2 + 1)

Page 34: Variables, Assignment & Math Storing and naming data

Remainder

• modulus (remainder) operator pairs with integer division…

• I have 37 people, how many groups of 5 can I make?

37 / 5 7 … 7 full groups of 5

37 % 5 2 … 2 people left over