Arithmetic Expressions in C CSCE 106 Outline n

  • Slides: 15
Download presentation
Arithmetic Expressions in C++ CSCE 106

Arithmetic Expressions in C++ CSCE 106

Outline n Data declaration {section 2. 3} n Arithmetic operators in C++ {section 2.

Outline n Data declaration {section 2. 3} n Arithmetic operators in C++ {section 2. 6} n Mixed data type arithmetic in C++ n Arithmetic expressions in C++ n Operators’ precedence n Arithmetic expressions’ evaluation examples CSCE 106 2

Data Declaration Remember the memory anatomy? Address Contents n It is much easier to

Data Declaration Remember the memory anatomy? Address Contents n It is much easier to set aside memory with names (identifiers). x 0 -27. 2 n Thus the declaration statements are used to set aside memory with a specific name for the n data and define its values and operations. 1 354 n type identifier-list; n Examples: char response; char c, grade = ‘A’; int min. Element; int n, j = 1; float score; float x, y, z = 40. 0; bool flag; n The value can change during execution CSCE 106 c 2 3. . . 1024 H ADD 7. . . 75. 62 3

Data Declaration (cont’d) When variables are declared 1. Memory allocated for value of specified

Data Declaration (cont’d) When variables are declared 1. Memory allocated for value of specified type 2. Variable name associated with that memory location 3. Memory initialized with values provided (if any) CSCE 106 27 4

Constant Declarations A constant is a memory cell whose value cannot change during execution

Constant Declarations A constant is a memory cell whose value cannot change during execution once it is set in the declartion. const type constant-identifier = value; n E. g. : const float KM_PER_MILE = 1. 609; n It is a good C++ programming practice to capitalize all letters for constant names. n Any data type (e. g. int, float, char, or bool) could be declared as constant. n CSCE 106 5

Identifiers n. A valid identifier (variable or function name) must consist of letters, digits,

Identifiers n. A valid identifier (variable or function name) must consist of letters, digits, or underscore only. n A valid identifier must not begin with a digit. n Valid identifiers: letter, letter 1, _letter n Invalid identifiers: 1 letter, float, hell o n You cannot use a C++ reserved word as an identifier. n Always try to associate meaningful identifiers. n It is a good C++ programming practice to capitalize the first letter of each word (after the first) when using multiple word identifiers n Remember that C++ is case sensitive (cost != Cost) CSCE 106 6

Arithmetic Operators in C++ + * / % n Addition Subtraction Multiplication Division Modulus

Arithmetic Operators in C++ + * / % n Addition Subtraction Multiplication Division Modulus (remainder operator, only for integers) Examples of integer modulus: 7%2=1 299 % 100 = 99 49 % 5 = 4 15 % 0 undefined n Examples of integer division (result is integer): 15 / 3 = 5 15 / 2 = 7 0 / 15 = 0 15 / 0 CSCE 106 undefined 7

Mixed Data Type Arithmetic in C++ n Example: 4. 6 / 2 evaluates to

Mixed Data Type Arithmetic in C++ n 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 8

Arithmetic Expressions in C++ An assignment statement has the following format: variable = expression;

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 u Then the result is stored in the variable on the left side of the assignment operator u CSCE 106 9

n Arithmetic Expressions in C++ (cont’d) Mixed-type assignments: u If the variable on left

n Arithmetic Expressions in C++ (cont’d) Mixed-type assignments: u 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. u Examples: float a, b, x; int m, n; a = 10; b = 3. 5; m = 5; n = 10; x = m / n; m = b * 3; CSCE 106 // result is 10. 0 stored in a // result is 0 assigned to x; therefore x is 0. 0 // result is 10 assigned to m 10

Arithmetic Expressions in C++ (cont’d) n What if the arithmetic expression contains more than

Arithmetic Expressions in C++ (cont’d) n 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? n Computers follow operator precedence rules to evaluate expressions. u The formula: m = y-b x-a u The computer equivalent is: m = (y - b) / (x - a); Must use ( ) because of operator precedence. n CSCE 106 11

Order of Operator Precedence Highest Lowest ( ) nested expressions evaluated inside out Associativity

Order of Operator Precedence Highest Lowest ( ) nested expressions evaluated inside out Associativity unary +, *, /, % If there are several, then evaluate from left-to-right binary +, If there are several, then evaluate from left-to-right = Warning: watch out for the types of operands and the type of the result from evaluating each operation! CSCE 106 12

Example 1 x = z - (a + b / 2) + w *

Example 1 x = z - (a + b / 2) + w * -y; 8 - (3 + 9 / 2) + 2 * --5 5 / 4 + 7 - * 10 1 + 11 CSCE 106 x integer z 8 a 3 b 9 w 2 y -5 x = 8 - (3 + 9 / 2) + 2 * - -5 9/2 (3 + 4 ) 87 +2*--5 87 +2* 5 87 + 10 11 13

Example 2 m= x + 5. 5 + k / 5 / 2; m

Example 2 m= x + 5. 5 + k / 5 / 2; m integer 2 x 5. 5 k / 5 2 convert to float 2. 0 + 7. 5 convert to int 7 CSCE 106 14

Next lecture will be about Library Functions CSCE 106 15

Next lecture will be about Library Functions CSCE 106 15