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
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
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'
For now
• Two "main" types– Whole numbersint
– Decimal numbersdouble
Literal Expressions
• Examples of int literals (values):-6728
0
78
+763
• Cannot use a comma within a number:10,000 NO
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
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
Identifiers
• Identifier : the name given be programmer to something in the program
double diameter; diameter
?? Value ??
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
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
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;
• = 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
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
• 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
• Complex assignment statement:
x = 16;
• Two steps1. Expression on right is evaluated 2. Value is copied to variable on left side
Assignment Statements
X
• 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
Using the Value of an Expression
• Variable NOT on left side of = is read from:
int y = 3;int x = y + 5;
y
Using the Value of an Expression
• Variable NOT on left side of = is read from:
int y = 3;int x = y + 5;
y
3
Using the Value of an Expression
• Variable NOT on left side of = is read from:
int y = 3;int x = y + 5;
y
3
Using the Value of an Expression
• Variable NOT on left side of = is read from:
int y = 3;int x = 3 + 5;
y
3
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
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"
This Makes Sense
x = x + 1;
This Makes Sense
x = x + 1;
x
8
This Makes Sense
x = 8 + 1;
x
8
This Makes Sense
x = 9;
x
9
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;
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
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
Division
…except•Dividing integers gives integer answer:
1 / 3 is 0
•Dividing doubles (decimals) gives decimal answer:
1.0 / 3.0 is 0.33333….
Mixed Types
• Integer combined with double makes a double:
1 / 3.0 is 0.3333
1.0 / 3 is 0.3333
Multiplication
…and•No implicit multiplication
NO: 3(2 + 1)
YES: 3 * (2 + 1)
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