C++Variables ◆C++ldentifiers Keywords/reserved words vs.Identifiers Case-sensitivity and validity of identifiers ◆Meaningful names! ◆Variables A memory location to store data for a program Must declare all data before use in program Copyright006 Pearson Addison-Wesley.All rights reserved. 1-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-6 C++ Variables C++ Identifiers Keywords/reserved words vs. Identifiers Case-sensitivity and validity of identifiers Meaningful names! Variables A memory location to store data for a program Must declare all data before use in program
Data Types: Display 1.2 Simple Types (1 of 2) Display 1.2 Simple Types TYPE NAME MEMORY USED SIZE RANGE PRECISION short 2 bytes -32,767t032,767 Not applicable (also called short int) int 4 bytes -2,147,483,647t0 Not applicable 2,147,483,647 long 4bytes -2,147,483,647t0 Not applicable (also called 2,147,483,647 long int) float 4bytes approximately 7 digits 1038to1o38 double 8 bytes approximately 15 digits 103o8to103o8 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 1-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-7 Data Types: Display 1.2 Simple Types (1 of 2)
Data Types: Display 1.2 Simple Types(2 of 2) long double io bytes approximately 9digits 104932t0104932 char I byte All ASCII characters Not applicable (Can also be used as an integer type, although we do not recommend doing s0.) bool i byte true,false Not applicable The values listed here are only sample values to give you a general idea of how the types differ. The values for any of these entries may be different on your system.Precision refers to the num- ber of meaningful digits,including digits in front of the decimal point.The ranges for the types float,double,and Long double are the ranges for positive numbers.Negative numbers have a similar range,but with a negative sign in front of each number. Copyright2006 Pears on Addison-Wesley.All rights reserved. 1-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-8 Data Types: Display 1.2 Simple Types (2 of 2)
Assigning Data Initializing data in declaration statement Results "undefined"if you don't! ◆int myValue=O; Assigning data during execution Lvalues (left-side)&Rvalues (right-side) Lvalues must be variables Rvalues can be any expression ◆Example: distance rate time; Lvalue:"distance" Rvalue:"rate time" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 1-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-9 Assigning Data Initializing data in declaration statement Results "undefined" if you don’t! int myValue = 0; Assigning data during execution Lvalues (left-side) & Rvalues (right-side) Lvalues must be variables Rvalues can be any expression Example: distance = rate * time; Lvalue: "distance" Rvalue: "rate * time
Assigning Data:Shorthand Notations ◆Display,page14 EXAMPLE EQUIVALENT TO count +=2; countcount +2; total-=discount; total total-discount; bonus *=2; bonus bonus 2; time /rushFactor; time time/rushFactor; change %100; change change 100; amount *cntl +cnt2; amount amount (cntl cnt2); Copyright2006 Pears on Addison-Wesley.All rights reserved. 1-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-10 Assigning Data: Shorthand Notations Display, page 14