Download presentation
Presentation is loading. Please wait.
1
Data Types, Variables & Arithmetic
2
C++ Integer Types Data Size in signed unsigned Type bytes Range Range
short ± 32, to 65,553 int ~ ±2 billion 0 to ~4 billion long (same as int, currently) long long ~ ± 9x to ~1.8x1019 On a 64-bit CPU. Size/Range vary by CPU model and Word size.
3
C++ Integer Types signed vs. unsigned unsigned short x; //range 0 to signed short x; //range ± short x; //assumed signed
4
C++ Float Types Type Size # significant digits float 4 bytes 7
double 8 bytes There are (usually) no unsigned floats or doubles. Using unsigned float in a program will result in: - a compiler error (must be corrected) or - a compiler warning (compiler ignores unsigned)
5
Floats are stored in scientific notation as two integer values
C++ Float Types Floats are stored in scientific notation as two integer values Mantissa: digits with an assumed radix point after the first digit. Exponent: (actually an exponent of 2 in binary) Example: (in decimal) = x 103 Mantissa = Exponent = 3
6
Addition in Scientific Notation
C++ Float Types Addition in Scientific Notation x Exponents must match! x (4 - 2 = 2) x Move radix point, subtract from exponent x 102 x Add the mantissas, keep the common exponent
7
A CPU must go through a very similar process to add two Floats!
C++ Float Types Addition in Scientific Notation x 102 x Convert the result to Scientific Notation x Round off to 7 significant digits x Done! A CPU must go through a very similar process to add two Floats!
8
Integer vs. Float Consideration Integer Float Increasing Size
increases Range increases number of significant digits Arithmetic fast and easy slow and complicated Precision always precise imprecise due to max number of significant digits Range Limited (small) Unlimited (practically)
9
General Design Principles
Integer vs. Float General Design Principles - Use Integers unless Floats are needed - Use larger Integer types when increased Range (larger numbers) is needed - Use larger Float types when increased Precision (number of significant digits) is needed.
10
Character Types char exactly one character string a sequence of 0 or more characters String variables are not native to C++; they are an Extension. To use this type, you must: #include <string>
11
Variable A variable is a container for data that has a
NAME: an identifier (created by the programmer) (DATA) TYPE: describes data values and operations CONTENTS: can change during execution of the program Variables must be Declared before used!
12
Declaration Statement
Syntax: type identifier; type identifier = literal; type id1, id2, id3; type id1 = lit1, id2 = lit2, id3 = lit3;
13
Declaration Statement
Semantics: type identifier = literal; - Creates a new variable named identifier of type type - Sets the initial value of the variable to literal
14
Declaration Statement
Semantics: Example: int a = 3; 1. Memory is allocated (“set aside”), the size of an int. 2. The location is “named” a. 3. The value 3 is placed in the location.
15
Declaration Statement
Style: Create meaningful variable names. double length, width, area; Not double l, w, a;
16
Declaration Statement
Examples: int length; long long width = 0; float radius = 3.25; double length = 2.5, width = 3.75, height = 0; char letterGrade = ’A’; string our_cheer = ”Go Big Blue!”;
17
Assignment Statement Purpose: change the value of a variable Syntax: variable = expression; - The variable must already be declared - The expression must evaluate to a value that is compatible with the type of the variable.
18
Assignment Statement Syntax: “compatible” usually means
“same type”, as for char and string. char letterGrade = ’A’; string our_cheer = ”Go Big Blue!”; Not char letterGrade = ”A”; string our_cheer = ’Go Big Blue!’; letterGrade = our_cheer;
19
Assignment Statement Syntax: floats and integers are generally cross-“compatible” syntactically, but semantically there are caveats ("dangers"). double y = 3; // no problem short x = 2.5;//OK, BUT see below
20
Assignment Statement Semantics: variable = expression; - Evaluate the expression on the right. - When the types differ but are compatible, convert the expression’s type to the variable’s type. - Change the current value of the variable to the evaluated result.
21
Assignment Statement Semantics: Warning on Float-to-Integer! Float values are truncated (not rounded). int x = 2.9; // sets x to 2 double y = 3.6; int z = y; // sets z to 3 cout << x << " " << y; Prints: 2 3
22
Arithmetic Operators +, -, *, / work as expected except:
- integer / integer results in an integer - Mixed operands (one int, one float) always results in a float. double x = 2/3; // int/int->int cout << x; // prints 0 x = 2/3.0; // int / float -> float cout << x; // prints
23
Arithmetic Modulus Operator for integers: % int dividend, remainder;
dividend = 7 / 5; // dividend = 1 remainder = 7 % 5; // remainder = 2
24
Arithmetic Order of Precedence: First: * , / , % left to right
Second: + , - left to right int x; x = * 4 – 6 / 3; // x = 11 // x = 5 + (2*4) – (6/3) // x = – 2 // x = 11
25
Arithmetic Parentheses may be used double x;
x = (3 + 2) * 5; // x = 25 X = * 5; // x = 13
26
Arithmetic Raising to an Exponent, Square Root, Round double x = sqrt(16); // x = 4 double y = pow(3,2); // y = 9 double z = round(2.8); // z = 3.0 double w = round(2.2); // w = 2 double d = round( * 100) / 100; // d = 3.12
27
Abbreviated Assignment Increment and Decrement
int y=0, x=0; x++; // x = x + 1; ++x; // x = x + 1; y = x++; // y = x; x = x + 1; y = ++x; // x = x + 1; y = x; x--; // x = x – 1; --x; // x = x – 1;
28
Abbreviated Assignment += -= *= /=
int X=1; X += 2; // x = x + 2; X -= 2; // x = x – 2; X *= 2; // x = x * 2; X /= 2; // x = x / 2;
29
Constants Definition: An identifier with a value that can never change. Syntax and Semantics: Same as a variable declaration with an initial value, but with the reserved word const in front: const type identifier = literal;
30
Constants Example: const double tax_rate = 0.06; … // syntax error: value cannot be changed tax_rate = 0.07;
31
Constants Why use constants? 1. Clarity 2. Consistency
3. Maintainability
32
Constants Clarity – how well a programmer can understand a program due to use of good style. extra = subtotal * 0.06; What is 0.06? A tax rate? Interest rate? Fee rate? const float tax_rate = 0.06; … tax = subtotal * tax_rate; Now we know!
33
Constants Consistency – when a value that should be exactly the same throughout a program actually is. area = 3.14 * radius * radius; circumference = 2 * * radius; sphere_surface = 4 * * radius * radius; The value of π is not consistent…this is better: const double pi = ; area = pi * radius * radius; circumference = 2 * pi * radius; sphere_surface = 4 * pi * radius * radius;
34
Constants Maintenance Any change to a program after it is “finished”. Maintainability The ease/speed of doing Maintenance.
35
Constants Maintainability Example In 2013, by coincidence, the state and local rates were the same state_tax = subtotal * 0.06; local_tax = subtotal * 0.06; In 2016, the local tax rate changed to 0.07 (but the state rate remained at 0.06) Now we have to go through all programs, find all 0.06’s and decide if each needs to change to 0.07 or not.
36
Constants Maintainability Example This would have been better: const double state_tax_rate = 0.06; const double local_tax_rate = 0.06; … state_tax = subtotal * state_tax_rate; local_tax = subtotal * local_tax_rate; Now, all we have to do is change the constant’s value in one place, and re-compile all programs that use it.
37
Vocabulary Term Definition Variable
Container for data: has a name, type and current contents. Declaration Statement that defines a variable (name, type, initial variable) and allocates the variable during execution. Allocate During execution, the reservation of memory location(s) represented by a variable. Assignment Statement that changes the value (current contents) of a variable. Mantissa The significant digits of a number in Scientific Notation, with an assumed radix point after the first digit. Modulus Operator that calculates the remainder of an integer division. % in C++ Constant An identifier with a declared type and value that cannot change.
38
Vocabulary Term Definition Clarity
How well a programmer can understand a program due to use of good style. Consistency When a value that should be the same throughout a program actually is. Maintenance Any changes to a program after it is “finished”. Maintainability The ease/speed of changing a program after it is “finished”.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.