1 / 34

Variables & Data Types

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

Download Presentation

Variables & Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables & Data Types 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

  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 numbersint • Decimal numbersdouble

  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 doubleliterals (values): 12 12.21 -3.1 1.5E12 //1.5 x 1012 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; doubleinterestRate; • No idea what is in allocated memory • Variables start with randomish values!!!

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

  10. 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

  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. = 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"

  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. Assignment Statements • Complex assignment statement: x = 12 + 4; • Two steps • Expression on right is evaluated • Value is copied to variable on left side

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

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

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

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

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

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

  22. Using the Value of an Expression • Variable NOT on left side of = is read from: int y = 3;int 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;

  26. This Makes Sense x = 8 + 1;

  27. This Makes Sense 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

More Related