Lesson 8.1: Arithmetic and Operators – Decimal
In our last lesson we took a brief look at what variables are. In this lesson we’ll look at how to do something with those variables, as well as general numbers by taking a look at how to do some basic arithmetic with common operators.
Arithmetic and Operators
Arithmetic is basically just the manipulation of numbers, as I’m sure you know. We all started very early in our math classes. Operators, as you may or may not remember from those classes, is simply the symbol that tells you what kind of operation to perform, such as the plus sign for addition.
In computer programming these definitions still apply. We also expand these to allow us to do “arithmetic” on data that isn’t necessarily a number (such as strings).
Types of Arithmetic
The typical arithmetic we use in our math class is called Decimal arithmetic. It can have whole or decimal numbers and uses the basic operators. In computer programming we have two more types of arithmetic: integer and boolean. Integer arithmetic is nearly identical to decimal arithmetic, with the exception that we truncate (cut off) everything after the decimal place and only deal with whole numbers.
Boolean arithmetic is where we look at true/false values and determine whether they equal true or false. We only have three operators for boolean arithmetic: and, or, xor.
First we’ll discuss decimal arithmetic.
Common Operators
In programming languages there are a number of operators. Some operators have multiple uses or different uses in different languages. For example, the parentheses are used in most languages for specifying the function parameters, and also for grouping in our arithmetic, just like you would do in your math class.
The common operators found in nearly all programming languages are the plus sign (+) for addition, the hyphen (-) for subtraction, the asterisk (*) for multiplication, and the slash (/) for division. They work exactly as you might expect them to work. Here are a few examples, using just numbers:
Arithmetic
5+5 equals 10 2-1 equals 1 10/2 equals 5 5*5 equals 25
No different then basic paper and pencil math. We also use the equals sign (=) for assigning. This is where things get a bit different from paper and pencil math.
Since the computer will figure out the answer on it’s own, we don’t have to say “5+5=10″, so we don’t use the equal sign for this purpose. We use the equals sign for assignment, that is, assigning a value to a variable. So, say we have a variable named sum. If we wanted it to hold 5+10, we would do something like this (in most languages):
Code
sum = 5 + 10
Take note that in nearly all languages, the variable that we are assigning a variable to must be on the left side of the equals (called the left-hand operand). You can also use variables on the right-hand side. These values won’t be changed and will simply supply a value to the equation. Let’s say we have something like this:
Code
a = 10 b = 5 sum = a+b
As you probably guessed, we’d end up with a sum equal to 15, while a would remain 10 and b would remain 5.
Order of Operations
The examples we looked at a moment ago were quite simple, with just a single operator in equation. You can however have equations with (theoretically) infinite numbers of operators. When you do, nearly all programming languages will follow the same order of operations that you learned in your math class. (I say “nearly all” because it isn’t a rule that it must follow the order of operations, but I don’t personally know of any languages that don’t follow it.)
For those of you that don’t remember, the basic order of operations is: parentheses (), multiplication or division, addition or subtraction. Something to keep in mind with programming is that square brackets ([]) usually have a different meaning in programming and aren’t just synonyms for parentheses like they are in mathematics.
Let’s work a few problems to look at the order of operations:
Order of Operations Examples
value = 5 + 9 / 3 * 5 + (10+5 * 3) value = 5 + 9 / 3 * 5 + (10 + 15) value = 5 + 9 / 3 * 5 + 25 value = 5 + 3 * 5 + 25 value = 5 + 15 + 25 value = 45 value = 10 * 3 + (50 / 4) + (50 + 3) - 10 / 4 + 5 * 3 value = 10 * 3 + 12.5 + (50 + 3) - 10 / 4 + 5 * 3 value = 10 * 3 + 12.5 + 53 - 10 / 4 + 5 * 3 value = 30 + 12.5 + 53 - 10 / 4 + 5 * 3 value = 30 + 12.5 + 53 - 2.5 + 5 * 3 value = 30 + 12.5 + 53 - 2.5 + 15 value = 42.5 + 53 - 2.5 + 15 value = 95.5 - 2.5 + 15 value = 93 + 15 value = 108 value = 3 + ((5+1) - (10/2) * (8*3)) value = 3 + (6 - (10/2) * (8 * 3)) value = 3 + (6 - 5 * (8 * 3)) value = 3 + (6 - 5 * 24) value = 3 + (6 - 120) value = 3 + (-114) value = -111
So, to figure out which one to do first we simply start at the left and work our way right. At first, we look for parentheses. When we find a set of parentheses, we treat it as a separate smaller equation and do our order of operations on it, reducing it to a single value, before we move back outside of them.
Once we can’t find any more parentheses, we go back to the left again and start looking for multiplication or division signs. Whenever we find one of these, we work it, then move on to the next one. Once we’ve found all of them, we look for addition and subtraction, and do each of them in the order we find them, until we’ve reduced it down to a single value.
And, of course, the computer doesn’t show it’s work. It just solves the entire equation really quickly and stores it in the variable.
Unknowns
In algebra, you would call a variable you don’t know it’s value an unknown. Usually your assignment would involve solving for these unknowns. So, you may find this in your fifth grade algebra book:
Algebra
Solve for the unknown "x": 10 = 5*x + 3
And you would have to work out what x is by working on both sides of the equations:
Algebra
10 = 5*x + 3 10-3 = 5*x + 3 - 3 7 = 5*x 7/5 = 5*x/5 1.4 = x
In programming, you can’t do this. You can’t use unknowns in your equation anywhere other than as the left-hand operator. So, if you wanted to solve for an unknown, you would have to rewrite the equation so the unknown is on the left-hand side. So, you could write that equation as x = (10-3)/5. The computer can easily solve this.
If you try to put an unknown on the right-hand side, it will almost invariably give you an error.
In this lesson we discussed the basics of arithmetic and took a look at examples of decimal arithmetic. In our next lesson we’ll wrap up this section with integer and boolean arithmetic.




