200 likes | 395 Views
Arithmetic Expressions in C++. Outline. Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in C++ Arithmetic expressions in C++ Operators’ precedence Arithmetic expressions’ evaluation examples. Remember the memory anatomy?
E N D
Outline • Data declaration {section 2.3} • Arithmetic operators in C++ {section 2.6} • Mixed data type arithmetic in C++ • Arithmetic expressions in C++ • Operators’ precedence • Arithmetic expressions’ evaluation examples CSCE 106
Remember the memory anatomy? It is much easier to set aside memory with names (identifiers). Thus the declaration statements are used to set aside memory with a specific name for the data and define its values and operations. type identifier-list; Examples: char response; char c, grade = ‘A’; int minElement; int n, j = 1; float score; float x, y, z = 40.0; bool flag; The value can change during execution Data Declaration Address Contents -27.2 x 0 354 n 1 H c 2 ADD 7 3 . . . . . . 75.62 1024 CSCE 106
27 Data Declaration (cont’d) When variables are declared • Memory allocated for value of specified type • Variable name associated with that memory location • Memory initialized with values provided (if any) CSCE 106
Constant Declarations • A constant is a memory cell whose value cannot change during execution once it is set in the declartion. consttype constant-identifier = value; • E.g.: constfloat KM_PER_MILE = 1.609; • It is a good C++ programming practice to capitalize all letters for constant names. • Any data type (e.g. int, float, char, or bool) could be declared as constant. CSCE 106
Identifiers • A valid identifier (variable or function name) must consist of letters, digits, or underscore only. • A valid identifier must not begin with a digit. • Valid identifiers: letter, letter1, _letter • Invalid identifiers: 1letter, float, hell o • You cannot use a C++ reserved word as an identifier. • Always try to associate meaningful identifiers. • It is a good C++ programming practice to capitalize the first letter of each word (after the first) when using multiple word identifiers • Remember that C++ is case sensitive (cost != Cost) CSCE 106
Arithmetic Operators in C++ + Addition - Subtraction * Multiplication / Division % Modulus (remainder operator, onlyfor integers) • Examples of integer modulus: 7 % 2 = 1 299 % 100 = 99 49 % 5 = 4 15 % 0 undefined • Examples of integer division (result is integer): 15 / 3 = 5 15 / 2 = 7 0 / 15 = 0 15 / 0 undefined CSCE 106
Mixed Data Type Arithmetic in C++ • Example: 4.6 / 2 evaluates to 2.3 Rule: when an integer and a floating point operand are combined by an operator, the integer gets converted to the floating point type. CSCE 106
Arithmetic Expressions in C++ An assignment statement has the following format: variable = expression; e.g.: kms = KM_PER_MILE * miles; • The arithmetic expression to the right of the assignment operator (=) gets evaluated first • Then the result is stored in the variable on the left side of the assignment operator CSCE 106
Arithmetic Expressions in C++ (cont’d) • Mixed-type assignments: • If the variable on left side of assignment is of different type than the type of the evaluated expression on the right side of =, the result of the expression must be converted to the appropriate type. • Examples: float a, b, x; int m, n; a = 10; // result is 10.0 stored in a b = 3.5; m = 5; n = 10; x = m / n; // result is 0 assigned to x; therefore x is 0.0 m = b * 3; // result is 10 assigned to m CSCE 106
Arithmetic Expressions in C++ (cont’d) • What if the arithmetic expression contains more than one operator? x = z - (a + b / 2) + w * -y; • How many operators do we have in the above expression? • Computers follow operator precedence rules to evaluate expressions. • The formula: m = y - b x - a • The computer equivalent is: m = (y - b) / (x - a); Must use ( ) because of operator precedence. CSCE 106
Order of Operator Precedence Highest Associativity ( ) nested expressions evaluated inside out unary +, - *, /, % If there are several, then evaluate from left-to-right binary +, - If there are several, then evaluate from left-to-right = Lowest Warning: watch out for the types of operands and the type of the result from evaluating each operation! CSCE 106
Example 1 x = z - (a + b / 2) + w * -y; x integer z a b w y 8 8 - (3 + 9 / 2) + 2 * --5 3 9 2 -5 - / 5 4 x = 8 - (3 + 9 / 2) + 2 * - -5 9 / 2 (3 + 4 ) 8 - 7 + 2 * - - 5 8 - 7 + 2 * 5 8 - 7 + 10 1 + 10 11 + * 7 10 - 1 + 11 CSCE 106
Example 2 m = x + k / 2; m integer x k 5.5 + 5 / 2 5.5 5 / 2 convert to float 2.0 + 7.5 convert to int 7 CSCE 106
Next lecture will be about Library Functions CSCE 106