Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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)

2 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

3 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

4 Mixed-type Expressions
Example: / 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

5 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!)

6 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

7 Order of Operator Precedence
Highest ( ) nested expressions evaluated inside out unary +, - *, /, % binary +, - Associativity Lowest Warning: watch out for the types of operands and the type of the result from evaluating each operand!

8 Step-by-Step Expression Evaluation

9 Figure 2.10 Evaluation for z - (a +b / 2) + w * -y;

10 Figure 2.11 Evaluation tree for m = x + k / 2:

11 Mathematical Formulas in C++
a = bc not valid C++ syntax Must use * operator a = b * c; m= y − b x − a Must use ( ) and / m = (y - b) / (x - a);


Download ppt "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."

Similar presentations


Ads by Google