Expressions An expression is a sequence of operands and operators that reduces to a single value. 5 + 2 expression operator operand An operator is a language-specific.
Published byModified over 4 years ago
Presentation on theme: "Expressions An expression is a sequence of operands and operators that reduces to a single value. 5 + 2 expression operator operand An operator is a language-specific."— Presentation transcript:
Expressions An expression is a sequence of operands and operators that reduces to a single value. 5 + 2 expression operator operand An operator is a language-specific syntactical token that requires an action to be taken. There is no limit to the number of operator and operand sets that can be combined to for an expression. An operand receives an operator’s action. There may be one, two or more operands in an expression. An operand may be constants, variables or other arithmetic expressions.
Expressions can be classified into several different formats Primary Postfix Unary Binary Ternary Assignment Comma identifer, constant, or parenthetical expression primary expression operator postfix expression unary expression operator binary expression operator expression operator expression variable operator expression operator expression Expressions Primary expressions are the most elementary type. A primary expression consists of only one operand with no operator. It can be either a name, a constant or a parenthetical expression. identifermiles price calc_tax constant56 ‘Z’ 245.78 parenthetical expression( 4 * 9 / 2 )
Expressions Binary Expressions are formed by an operand-operator - operand combination. Some of the binary operators The value of these expressions are the same type as the type of the operands in the expression. The result of the modulus operator can only be an integer.
Expressions Examples of binary expressions 2 + 1/* the result is 3 */ 5 * 6 /* the result is 30 */ 5 / 2 /* the result is 2 */ 3 / 5 /* the result is 0 */ 7 % 4 /* the result is 3 */ Assignment Expressions evaluates the operand on the right side of the assignment operator ( = ) and places its value in the contents of the memory cell represented by the variable on the left hand side. The operand on the left hand side of the assignment operator must be a variable. The value of the total expression is the value of the expression on the right of the assignment operator. The assignment the value to the variable on the left hand side is called the side effect. The expression x = 3 * 2 is read, x “gets” or “takes the value of” rather than “equals” because it is not equivalent to the “equal sign” of mathematics.
Expressions Compound assignment is shorthand notation for a simple assignment. It requires that the left operand be repeated as a part of the right expression. There are 5 compound assignments operators *=, /=, %=, +=, -=. Example x = x + 2 can also be expressed as x += 2 both of these expression achieve the same result and value. Example int x = 10; int y = 5; printf(“x = %d and y = %d\n”, x,y); printf(“the value of x *= y is %d\n”, x *= y); printf(“the value of x is now %d\n”, x,); the output is x = 10 and y = 5 the value of x *= y is 50 the value of x is now 50
Postfix expressions consists of one operand, which must be a primary expression, followed by one operator. Two of the postfix operators are the increment and decrement operators. Expressions In the postfix increment the value of the expression is the current value of the variable and the side effect is that the variable is increased by one. In the postfix decrement the value of the expression is the current value of the variable and the side effect is that the variable is decreased by one. Example a++
Unary expressions consists of one operator and one operand. Expressions Example ++a In the prefix increment the value of the expression is the current value of the variable plus one and the side effect is that the variable is increased by one. In the prefix decrement the value of the expression is the current value plus one of the variable and the side effect is that the variable is decreased by one.
Expressions The sizeof operator looks like a function but it is actually an operator. Its purpose matches its name: It tells you the size, in bytes, of whatever type is specified. Example sizeof( int ); /* returns the number of bytes for a integer type */ sizeof( 23.56 ); /* returns the number of bytes needed to hold the expression */ Precedence Precedence is used to determine the order in which different operators in a complex expression are evaluated How should the following expression be evaluated: 2 + 3 * 4 should you do the addition first then the multiplication or the multiplication and then the addition. The difference is the result.
Precedence Rules for evaluating expressions: 1)Parentheses rule: All expressions in parentheses must be evaluated separately. Nested parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated first. 2)Operator precedence rule: operators in the same expression are evaluated in the following order: (see inside cover of text). 3)Associativity rule: unary operators in the same subexpression and at the same precedence level (such as + and - ) are evaluated right to left ( right associativity ). Binary operators I the same subexpression and at the same precedence level (such as *, /, and % ) are evaluated left to right (left associativity ). (see inside cover of text). Expressions Use extra parentheses to clarify the order of evaluation. 3 * 8 / 4 % 4 * 5 all operators are at the same precedence level so associativity determines the evaluation should be done from left to right. ( ( ( ( 3 * 8 ) / 4 ) % 4 ) * 5 )
Expressions Mixed type Expressions these are expressions which have two or more operands with different types. When this happens what it the type of the resulting value of the expression. Example 2 + 3.5 in the above example is the resulting value a floating point or 5.5 or an integer 5 ? C will automatically convert any intermediate values to the proper so that the expression can be evaluated. When C automatically converts a type from on format to another, it is known as implicit type conversion. Rules for conversion: 1)C uses the rule that in all expressions except assignments, any implicit type conversion will always be made to the more general type according to the promotion order shown below. 2With an assignment expression the final expression value must have the same type as the left operand or the operand that receives the value.
Expressions char Long double float unsigned long int unsigned int short Promotion Hierarchy Using this hierarchy, if we were to add an integer and a double and store the result into and integer, we would first convert the integer in the binary + expression to a double, because double is higher in the promotion hierarchy, and then after the addition, convert the result back into and integer for assignment to the integer variable.
Expressions Example of implicit type conversion: char ch; int i result; float f; double d; result = (ch / i ) + ( f * d ) - ( f + i ) ; intdoublefloat double int double
Expressions Explicit type conversion ( cast operator ) The explicit type conversion uses the cast expression operator. To cast data from one type to another your specify the new type in parentheses before the value you want converted. Example to convert the integer a to float: ( float ) a the operand must be a unary expression: Example to cast the expression x + y as a double: ( double ) ( x + y ) Example int a, ; float b; a = 5; b = ( float ) ( a / 10 ); /* b receives 0.0 */ b = ( float ) a / 10; /* b receives 0.5 */ a is explicitly converted to a float which causes 10 to be implicitly converted to a float which results in a value of type float.