Expressions An expression is a portion of a












- Slides: 12

Expressions • An expression is a portion of a C++ statement that performs an evaluation of some kind • Generally requires that a computation or data manipulation be performed to produce a result of a specified type • Arithmetic expression • Performs a numeric computation producing a numerical result • Logical expression • Produces a Boolean result evaluating to true or false • String expression • Produces a result of type string • Expressions may contain other expressions which are evaluated in order producing an overall result • Ex: (2+5) * (7+3) Professor John Carelli Kutztown University Computer Science Department

Arithmetic Expressions • Basic arithmetic operations + - * / • int data type includes % (modulus) • Integer division - result is integer 15 / 3 = 5 15 / 2 = 7 0 / 15 = 0 15 / 0 undefined • Float results includes decimal portion 15. 0 / 2. 0 = 7. 5 Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Modulus for Integers • Used only with integers • Get a compiler error if used with a float • Yields remainder - the result is integer • Examples: 7%2=1 299 % 100 = 99 49 % 5 = 4 15 % 0 undefined 15 % -7 system dependent Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Mixed-type Expressions • 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 • Caveat: this rule is dependent on operator precedence rules Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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 • Conversion from a float to an int involves dropping (truncating, not rounding!) the decimal part • Example: int n; float x=2. 7; n= x; // n will be assigned the value 2 (trick: to round, add 0. 5 first!) Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Mixed-type Assignment 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 m = b * 3; // result is 10 assigned to m Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Casting • Type casting allows for explicit conversion from one data type to another • Accomplish by using the type name directly int sum=10, n=4; float average= float(sum) / float(n); result is: 2. 5 (not 2!) Professor John Carelli Kutztown University Computer Science Department

Order of Operator Precedence Highest ( ) unary +, *, /, % binary +, - nested expressions evaluated inside out Associativity Lowest Warning: watch out for the types of operands and the type of the result from evaluating each operand! Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Step-by-Step Expression Evaluation float area, PI=3. 14159, radius; radius= 2. 0; Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 2. 10 Evaluation for z - (a +b / 2) + w * -y; int a=3, b=9, w=2, y=-5, z=8; Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Figure 2. 11 Evaluation tree for m = x + k / 2: int m, k=5; float x=5. 5; Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Mathematical Formulas in C++ • Professor John Carelli, Kutztown University Source: Pearson Education, Inc. Publishing as Pearson Addison-Wesley