An expression consist of one or more operands, zero or more operators linked together to compute value.
Examples of expressions
a
a + 2
a + b
x = y
15/20
There are four types of expressions
i. Constant Expression
ii. Integral expression
iii. Float Expressions
iv. Pointer Expressions
4.1 Constant Expression.
Constant expressions contain constant values. Examples of constant expressions are:
5
5 + 6 + 13/3.0
‘a’
4.2 Integral Expressions
Integral expressions that offer all the arithmetic and explicit type conversions, produce results that has one of the integer types. If j and k are integers then the following are all integral expressions.
j
j + k
j/k + 3
k – ‘a’
3 + (int)5.0
4.3 Float Expressions
These are the expressions that offer all the automatic and explicit type conversions, produce a result that has one of the floating type conversions. If x is a float or double, then the following are floating-point expressions:
x
x + 3
x + 3
x / y * 5
3.0
3.0 - 2
3 + (float)4
4.4 Pointer Expression
Pointer Expression
These are expressions that evaluate to an address value. These include expressions containing pointer variables, the address of operator (&), string literals, and array names. If p is a pointer and j is an int, the following are pointer expressions.
p &j
p + i
“abc”
4.5 Automatic Type Conversion in Expressions
When the compiler encounters an expression, it divides it into sub expressions, where each expression one operator and one operand. Each operator has its own rules for operand type agreement. However, most of the binary operators require both operands to have the same type. If types differ, the compiler converts one of the operands to agree with the other one. Floating points have higher hierarchy than integers. Thus, integers are converted to floating points. For Example:
1 + 2.5
In this expression the integer 1 is converted to float and the expression becomes
1.0 + 2.5
5.0 CONDITIONAL OPERATOR (?)
The conditional Operator:takes three operands. The value of an expression using the conditional operator is the value of either the second or the third operand, depending on the value of the first operand. For example consider the following expression:
z = ((x < y) ? x : y);
This expression means that:
if (x < y)
z = x;
else
z = y;
Note that in the expression above, the first operand is the test condition. The second and third operand represents the final value of the expression. The condition operator can be summarized as shown in table 8.
Table 8: Conditional Operators
| Operator | Symbol | Form | Results |
| Conditional | ?: | a ? b : c | 1 if a is non result is b: otherwise result is C |
No comments:
Post a Comment