CSCI 201 Study Aid: Arithmetic Operators

Arithmetic Operators

Most programs perform arithmetic calculations. The arithmetic operators are summarized in the table below:

C++
Operation
Arithmetic
operator
Algebraic
expression
C++
expression
Addition + f + 7 f + 7
Subtraction - p - c p - c
Negation - -x -x
Multiplication * bm b * m
Division / x / y x / y
Modulus % r mod s r % s

Except for the Negation operator, the arithmetic operators in the table above are binary operators, which means each operator requires two operands, for example: f + 7 (f and 7 are the operands and the + is the operator).

The negation operator is a unary operator, which means it requires only one operand, for example: -x (make the value of operand x negative).

Note that integer division yields an integer result; 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.

C++ provides the modulus operator, %, which yields the remainder after integer division. The modulus operator is an integer operator that can only be used with integer operands. The expression x % y yields the remainder after x is divided by y. For example, 7 % 4 yields 3, and 17 % 5 yields 2.


Arithmetic Expressions

The arithmetic operators may be combined to form expressions. For example, given the following declarations and expressions:

   int a;
   int u = 4;
   int x = 5
   int y = 10;
   int z = 2;

   a = z + u * y / x - u;

The value of a is evaluated using the rules of operator precedence and associatively below. Thus, the value of a in the example above would be 6. The expression can be re-written as shown below. Note the use of the parenthesis to force the order of evaluation according the the precedence rules:

   a = 2 + ( ( 4 * 10 ) / 5 ) - 4;  // Multiplication first (left to right)
   a = 2 + ( 40 / 5 ) - 4;          // Then division
   a = 2 + ( 8 ) - 4;               // Addition first (left to right)
   a = 10 - 4;                      // Subtraction
   a = 6;

Rules of operator precedence and Associatively

C++ applies the operators in arithmetic expressions in a precise sequence determined by the following rules, which are generally the same as those followed by algebra:

  1. Operators in expressions contained within pairs of parentheses are evaluated first. Thus, parenthesis may be used to force the order of evaluation to occur in any sequence desired by the programmer.

  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.

  3. Addition and subtraction operations are applied last. If an expression contains several addition and subtraction operations, operators are applied from left to right.

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 associatively of the operators. The following table summarizes the precedence and associatively of the arithmetic operators:

Operator(s) Operation(s) Order of evaluation (precedence)
( ) Parentheses Evaluated first. If the parenthesis are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses "on the same level" (i.e: not nested), they are evaluated left to right.
*, /, or % Multiplication
Division
Modulus
Evaluated second. If there are several, they are evaluated left to right.
+ or - Addition
Subtraction
Evaluated last. If there are several, they are evaluated left to right.

Assignment Operators

C++ provides several assignment operators for abbreviating assignment expressions. For example, the statement: Any statement of the form:

   object = object operator expression;

can be rewritten as:

   object operator= expression;

where operator is one of the binary operators +, -, *, /, or % (and others which are covered later). For example, the expression:

   n = n + 10;

can be rewritten as:

   n += 10;

The table below illustrates using these operators.
Assume: c = 3, d = 5, e = 4, f = 6, g = 12;

Assignment
Operator
Sample
Expression
Explanation Assigns
+= c += 7 c = c + 7 10 to c
-= d -= 4 d = d - 4 1 to d
*= e *= 5 e = e * 5 20 to e
/= f /= 3 f = f / 3 2 to f
%= g %= 9 g = g % 9 3 to g

Questions

State the order of evaluation of the operators in each of the following C++ statements, and show the value of x after each statement performed. Assume x is an integer object.

  1. x = 4 + 5 * 7 / 3 - 4;

  2. x = 9 % 9 + 7 * 6 - 3 / 2;

  3. x = (3 * 2 * ( 2 + ( 6 * 10 / ( 3 ) ) ) );
Solution