 Introduction to C# Applications

• Print
This chapter is from the book

3.7 Arithmetic

The arithmetic operators are summarized in Fig. 3.19. Note the various special symbols not used in algebra. The asterisk (*) indicates multiplication, and the percent sign (%) is the remainder operator (called modulus in some languages), which we'll discuss shortly. The arithmetic operators in Fig. 3.19 are binary operators—for example, the expression f + 7 contains the binary operator + and the two operands f and 7.

Fig. 3.19. Arithmetic operators.

 C# operation Arithmetic operator Algebraic expression C# expression Addition + f + 7 f + 7 Subtraction – p – c p - c Multiplication * b · m b * m Division / x / y Remainder % r mod s r % s

Integer division yields an integer quotient—for example, the expression 7 / 4 evaluates to 1, and the expression 17 / 5 evaluates to 3. Any fractional part in integer division is simply discarded (i.e., truncated)—no rounding occurs. C# provides the remainder operator, %, which yields the remainder after 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 most commonly used with integer operands but can also be used with floats, doubles, and decimals. We will consider several interesting applications of the remainder operator, such as determining whether one number is a multiple of another.

Arithmetic expressions must be written in straight-line form to facilitate entering applications into the 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 is generally not acceptable to compilers: Parentheses are used to group terms in C# expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c, we write

 ```a * ( b + c ) ```

If an expression contains nested parentheses, such as

 ```( ( a + b ) * c) ```

the expression in the innermost set of parentheses (a + b in this case) is evaluated first.

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 followed in algebra (Fig. 3.20).

Fig. 3.20. Precedence of arithmetic operators.

 Operators Operations Order of evaluation (associativity) Evaluated first * Multiplication If there are several operators of this type, they're evaluated from left to right. / Division % Remainder Evaluated next + Addition If there are several operators of this type, they're evaluated from left to right. - Subtraction

When we say that operators are applied from left to right, we're referring to their associativity. You'll see that some operators associate from right to left. Figure 3.20 summarizes these rules of operator precedence. We expand this table as additional operators are introduced. Appendix A provides the complete precedence chart.