Tuesday, January 14, 2014

Most programs perform arithmetic calculations. Figure 3.15 summarizes the arithmetic op-
erators. Note the use of various special symbols not used in algebra. The asterisk (*) indi-
cates multiplication, and the percent sign (%) represents the modulus operator, which is
discussed shortly. The arithmetic operators in Fig. 3.15 are binary operators, because they
each require two operands. For example, the expression sum + value contains the binary
operator + and the two operands sum and value.






     Integer  division  contains  two  int  operands.  The  result  of  this  computation  is  an
integer quotient; for example, the expression 7 / 4 evaluates to 1 and the expression 17 /
5 evaluates to 3. Note that any fractional part in integer division simply is discarded (i.e.,
truncated)—no rounding occurs. C# provides the modulus operator, %, which yields the
remainder  after  integer  division.  The  expression  x  %  y  yields  the  remainder  after  x  is
divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2. This operator is used most com-
monly with integer operands, but also can be used with other arithmetic types. In later chap-
ters, we  consider  interesting  applications  of the modulus operator, such  as  determining
whether one number is a multiple of another. There is no arithmetic operator for exponen-
tiation in C#. (Chapter 6, Methods, discusses how to perform exponentiation in C#.)
     Arithmetic expressions in C# must be written in straight-line form to facilitate entering
programs into a computer. Thus, expressions such as “a divided by b” must be written as
a/b so that all constants, variables and operators appear in a straight line. The following
algebraic notation generally is not acceptable to compilers:

     a
     b

     C# expressions can use parentheses in the same manner as in algebraic expressions.

For example, to multiply a times the quantity b + c, we write

     a * ( b + c )

     C# applies the operators in arithmetic expressions in a precise sequence, determined
by the following rules of operator precedence, which are generally the same as those fol-

lowed in algebra:

          1.  Operators in expressions contained within pairs of parentheses are evaluated first.
Thus, parentheses may be used to force the order of evaluation to occur in any se-
quence desired by the programmer. Parentheses are at the highest level of prece-
dence. With nested (or embedded) parentheses, the operators in the innermost pair
of parentheses are applied first.

         2.  Multiplication, division and modulus operations are applied next. If an expression
contains  several  multiplication,  division and  modulus operations,  operators are applied from left to right. Multiplication, division and modulus are said to have
the same level of precedence.

         3.  Addition and subtraction operations are applied last. If an expression contains sev-
eral addition and subtraction operations, operators are applied from left to right.
Addition and subtraction have the same level of precedence.

     The rules of operator precedence enable C# to apply operators in the correct order. When
we say operators are applied from left to right, we are referring to the associativity of the
operators. If there are multiple operators, each with the same precedence, the associativity
determines the order in which the operators are applied. We will see that some operators
associate from right to left. Figure 3.16 summarizes the rules of operator precedence. This
table will expand as we introduce additional C# operators in subsequent chapters. See Ap-
pendix A for a complete operator-precedence chart.

     Notice in the chart that we make note of nested parentheses. Not all expressions with
several pairs of parentheses contain nested parentheses. For example, the expression

          a * ( b + c ) + c * ( d + e )

has multiple sets of parentheses, but not nested parentheses. Rather, these parentheses are

said to be “on the same level.”
     Let us consider several expressions in light of the rules of operator precedence. Each
example lists an algebraic expression and its C# equivalent.
     The following is an example of an arithmetic mean (average) of five terms:

          Algebra:  =  a+b+c+d+e
                                5

          C #: m = ( a + b + c + d + e ) / 5 ;

The parentheses are required because division has higher precedence than addition. The en-
tire quantity ( a + b + c + d + e ) is to be divided by 5. If the parentheses are
erroneously omitted, we obtain a + b + c + d + e / 5, which evaluates as
          + b + c + d + e
                              5

     The following is the equation of a straight line:

          Algebra: y  =  mx + b
   
         C#:         y = m * x + b;


No parentheses are required. The multiplication occurs first because multiplication has a
higher precedence than addition. The assignment occurs last because it has a lower precedence than multiplication and division.
     The following example contains modulus (%), multiplication, division, addition and
subtraction operations:

          Algebra:  z  =  pr%q + w/x – y

          C#:         z  =  p  *  r  %  q  +  w  /  x  -  y;




The circled numbers under the statement indicate the order in which C# applies the opera-
tors. The multiplication, modulus and division operators evaluate first in left-to-right order
(i.e., they associate from left to right). The addition and subtraction evaluate next. These
also are applied from left to right.
     To develop a better understanding of the rules of operator precedence, consider how a
second-degree polynomial (y = ax2 + bx + c) evaluates:

         y  =  a  *  x  *  x  +  b  *  x  +  c;




     The circled numbers under the statement indicate the order in which C# applies the opera-
tors. There is no arithmetic operator for exponentiation in C#; x2 is represented as x * x.
The .NET Framework Class Library provides method Math.Pow for exponentiation (see
Chapter 6, Methods).
     Suppose a, b, c and x are initialized as follows: a = 2, b = 3, c = 7 and x = 5.
Figure 3.17 illustrates the order of evaluation of the operators.
     As in algebra, it is acceptable to place unnecessary parentheses in an expression to
make  the  expression  easier  to  read. Unnecessary  parentheses  are  also  called  redundant
parentheses. For example, the preceding assignment statement might be parenthesized as


          y = ( a * x * x ) + ( b * x ) + c;

          Good Programming Practice 3.13
Using parentheses for more complex arithmetic expressions, even when the parentheses are
not necessary can make the arithmetic expressions easier to read..



             

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!