390 likes | 695 Views
Variables & Data Types. 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
E N D
Variables & Data Types 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
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 doubleliterals (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; doubleinterestRate; • No idea what is in allocated memory • Variables start with randomish values!!!
Identifiers • Identifier : the name given be programmer to something in the program double diameter;
Identifiers • Identifier Rules • Consists of letters, digits, and the underscore character (_) • Must begin with a letter or underscore Yes: myNumbermy_Number x1 No: 1x my Numbermy-Number • C++ is case sensitive • NUMBER is not the same as number
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
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;
= is not equals • = does not mean "equals" • In C++, = is called the assignment operator x = 10;" x gets assigned the value 10""store 10 in location x"
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
Assignment Statements • Complex assignment statement: x = 12 + 4; • Two steps • Expression on right is evaluated • Value is copied to variable on left side
Assignment Statements • Complex assignment statement: x = 16; • Two steps • Expression on right is evaluated • Value is copied to variable on left side
Assignment Statements • Complex assignment statement: x = 16; • Two steps • Expression on right is evaluated • Value is copied to variable on left side
Using the Value of an Expression • Variable NOT on left side of = is read from: int y = 3;int x = y + 5;
Using the Value of an Expression • Variable NOT on left side of = is read from: inty = 3;int x = y + 5;
Using the Value of an Expression • Variable NOT on left side of = is read from: int y = 3;int x = y + 5;
Using the Value of an Expression • Variable NOT on left side of = is read from: int y = 3;int x = 3 + 5;
Using the Value of an Expression • Variable NOT on left side of = is read from: int y = 3;int 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;
This Makes Sense x = 8 + 1;
This Makes Sense 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…
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