Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables, Assignment & Math Storing and naming data.

Similar presentations


Presentation on theme: "Variables, Assignment & Math Storing and naming data."— Presentation transcript:

1 Variables, Assignment & Math Storing and naming data

2 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

3 Declaring Variables Variable : memory location whose content may change Declaring a variable allocates space and names that space counter 0101 0110 0111 0110 1101 0110 0101 0110 interestRate 0101 0110 1111 0100 1111 0110 0101 0000 1101 0110 0001 0111 1101 0111 0101 0110

4 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'

5 For now Two "main" types – Whole numbers int – Decimal numbers double

6 Literal Expressions Examples of int literals (values): -6728 0 78 +763 Cannot use a comma within a number: 10,000 NO

7 Literal Expressions Examples of double literals (values): 12 12.21 -3.1 1.5E12 //1.5 x 10 12 1.5E-1 //1.5 x 10 -1

8 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 0110 0111 0110 1101 0110 0101 0110 interestRate 0101 0110 1111 0100 1111 0110 0101 0000 1101 0110 0001 0111 1101 0111 0101 0110

9 Identifiers Identifier : the name given be programmer to something in the program double diameter; diameter ?? Value ??

10 Identifiers Identifier Rules – Consists of letters, digits, and the underscore character (_) – Must begin with a letter or underscore Yes: myNumber my_Numberx1 No: 1x my Numbermy-Number C++ is case sensitive – NUMBER is not the same as number

11 Naming Identifiers Identifiers should be self-documenting – Avoid single letters, excessive abbreviation YES: 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

12 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;

13 = 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

14 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

15 Complex assignment statement: x = 12 + 4; Two steps 1.Expression on right is evaluated 2.Value is copied to variable on left side Assignment Statements X

16 Complex assignment statement: x = 16; Two steps 1.Expression on right is evaluated 2.Value is copied to variable on left side Assignment Statements X

17 Complex assignment statement: x = 16; Two steps 1.Expression on right is evaluated 2.Value is copied to variable on left side Assignment Statements X 16

18 Using the Value of an Expression Variable NOT on left side of = is read from: int y = 3; int x = y + 5; y

19 Using the Value of an Expression Variable NOT on left side of = is read from: int y = 3; int x = y + 5; y 3

20 Using the Value of an Expression Variable NOT on left side of = is read from: int y = 3; int x = y + 5; y 3

21 Using the Value of an Expression Variable NOT on left side of = is read from: int y = 3; int x = 3 + 5; y 3

22 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

23 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"

24 This Makes Sense x = x + 1;

25 This Makes Sense x = x + 1; x 8

26 This Makes Sense x = 8 + 1; x 8

27 This Makes Sense x = 9; x 9

28 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;

29 Doing Math Math works as normal…

30 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

31 Division …except Dividing integers gives integer answer: 1 / 3 is 0 Dividing doubles (decimals) gives decimal answer: 1.0 / 3.0 is 0.33333….

32 Mixed Types Integer combined with double makes a double: 1 / 3.0 is 0.3333 1.0 / 3 is 0.3333

33 Multiplication …and No implicit multiplication NO: 3(2 + 1) YES: 3 * (2 + 1)

34 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


Download ppt "Variables, Assignment & Math Storing and naming data."

Similar presentations


Ads by Google