BAB V
Expression and Assignment
Statement
Arithmetic Expression
Operators
·
Unary, one operand
·
Binary, two operands
·
Ternary, three operands
Operator Precedence Rules
1.
Parentheses
2.
Unary operator
3.
*, /
4.
+, -
Associativity Rules, The operator associativity rules for
expression evaluation define the order in which adjacent operators with the
same precedence level are evaluated
Conditional Expression (IF statement)
Example
·
average = (count == 0)? 0 : sum / count
·
if (count==0)
{
Average=0;
}
Else
{
Average=sum/count;
}
Operand evaluation order
1. Variables:
fetch the value from memory
2. Constants:
sometimes a fetch from memory; sometimes the constant is in the machine
language instruction
3. Parenthesized
expressions: evaluate all operands and operators first
4. The most
interesting case is when an operand is a function call
Overloaded Operators
Use of an operator for more than one purpose is called
operator overloading
e.g. + for integer and float, 6 + 9.50.
A narrowing conversion is one that converts an object to a
type that cannot include all of the values of the original type e.g., float to
integer.
A widening conversion is one in which an object is converted
to a type that can include at least approximations to all of the values of the
original type e.g., integer to float.
Explicit type
conversions
Called casting in C-based languages
Examples
C : (int)angle
Implicit type
conversions
double F=9.4;
int Z=F;
Z will have a value of 9 rather than 9.4;
Relational Expressions are (!=, /=, ~=, .NE., <>, #)
Short Circuit
Evaluation
An expression in which the result is determined without
evaluating all of the operands and/or operators
Example: (13 * a) * (b / 13 – 1)
If a is zero, there is no need to evaluate (b /13 - 1)
Tidak ada komentar:
Posting Komentar