Lesson Summary

In this lesson, we expanded on the topic of operators to talk about those operators used when programming arithmetic into your expressions.

Many of the items covered in this example will have been familiar to anyone who has already studied arithmetic as part of their education, but it is important to understand the usage and syntax of all operators you may want to use in your expressions, whether you are putting together a basic arithmetic equation or combining multiple statements into a more complex piece of code:

Most arithmetic operators are binary, which means they require two numeric operands. The sole exception is the minus operator, which can be unary (one numeric operand) or binary, depending on what you want to use it for:

  • Addition: Takes two operands, combines their values together to produce a sum total.
  • Subtraction (binary minus): Takes two operands, reduces the value of the first operand by the second operand.
  • Negation (unary minus): Takes a single operand. If the operand is positive (greater than zero), it becomes negative, and vice versa. Negating zero has no effect.
  • Multiplication: Takes two operands and scales one by the other to give the product.
  • Division: Takes two operands and splits the first one (the numerator) evenly among a number of parts equal to the value of the second operand (the denominator). The value given to each part is called the quotient.
  • Integer Division: As with regular division, integer division splits its numerator evenly, the difference being that each share is then rounded down to the nearest whole number, effectively removing all fractions from the quotient.
  • Exponent: Raises the first operand (the base) to the power of the second operand (the exponent).
  • Parentheses: Overrides operator precedence and otherwise makes your code easier to read.

You should now fully understand the purpose of the arithmetic operators listed above, how to implement them, and the kind of expression they are useful for. You should also understand how operator precedence works and how to override it using parentheses.

 

<< Previous: Arithmetic Operator Precedence | Next: Test Your Knowledge! >>