Chapter 5: Math

When the computer sees a mathematical formula, it performs the arithmetic automatically. For example, you can write:

print 5 + 6

Note again that there are no quotation marks. If there were, the computer would take that literally and print “5 + 6”. The plus sign is called an operator because it performs operations on other values. When the computer sees the above line, it will perform the arithmetic and get:

print 11

Executing the statement will just print the number:

11

Note that the following two statements will display the same thing:

print 11
print "11"

except that in the first case the computer knows it’s a number, whereas in the second case it’s just a string. The math can get pretty complicated:

print (5 + 12) * 9 / 14

The computer would perform the arithmetic step by step, just like you would in your head. It would first do the addition, since it’s in parentheses:

print 17 * 9 / 14

Then the multiplication, since it’s on the left:

print 153 / 14

Then the division: (The slash means “divide” because there’s no “÷” key on the keyboard.)

print 10

And you’d see this if you ran the original line:

10

Note that 153 divided by 14 is actually something like 10.928, but the computer printed the round number below the real value. That’s because there are two kinds of numbers most languages deal with: integers and reals. Integers are round numbers, like the ones above. Reals are numbers with a decimal point, like 10.928. When the computer sees a formula using entirely integers, it keeps using integers to do the math, even if the result really should be a real. Had we written:

print (5.0 + 12.0) * 9.0 / 14.0

then the computer would have performed the math using reals and we would have seen something like this printed:

10.9285714286

This is a quirky result of the historical development of computers. It can lead to bizarre results, like this line:

print 1 / 2

displaying “0” instead of the expected “0.5”. There are times when the current behavior is desirable, but often it’s not and you should keep the distinction between integers and reals in mind. Also note that the above rules about integers and reals apply to most commonly-used programming languages, but each language is free to make up its own rules for dealing with numbers, and you may one day use a language that does things differently, such as making “1 / 2” result in “0.5”.

Variables can be used to refer to numbers as well as strings. You could write:

x = 5
print x

to print “5”. Or you could write:

age = 33
print age * 2

to find out twice your age. Again here the first line creates in memory an association between the variable whose name is age with the number 33. The second line looks into memory, grabs the number associated with age, and performs the arithmetic. Note that the first line is not algebra. It’s not an equation that makes age and the number 33 the same. It’s an assignment of the value 33 to the variable age. The distinction is important when you see a line like:

x = x + 5

The above line is impossible in algebra. The value of x cannot be equal to the value of x plus five. But in a programming language, the line reads as, “Find the value of the variable x in memory, add 5 to it, then associate the result with the variable x.” The fact that x is used both in the math formula on the right of the equal sign and as the place to store the results is irrelevant. Its value is used in the math formula before the result is stored back into the variable. On the left of the equal sign you can only put a single variable. That’s the variable that will be associated with whatever’s on the right of the equal sign. Think of the equal sign as a left-pointing arrow, which moves the value on its right-hand side to the variable on its left-hand side. In fact some languages, such as Pascal, use “:=” (which looks a bit like a left-pointing arrow) to make it more clear that data is being moved from the right to the left:

age := 33                  (in Pascal)

Here’s another example:

x = 5
y = x + 2
print x
print y

You’ll get:

5
7

But now if you write (the new line is highlighted):

x = 5
y = x + 2
x = 10
print x
print y

You’ll get:

10
7

Go through each line and “play computer” to convince yourself of why the above would behave this way. Use a piece of paper to keep track of the x and y variables if you need to. If the above isn’t clear, stop and re-read this chapter or the one on variables. This is a very common stumbling block for new programmers.

You might have noticed that we use the plus (+) symbol to mean both “string concatenation” and “numerical addition”. This is fine and usually clear, but what should the computer do with a statement like this:

print "5" + 6

Should the numbers 5 and 6 be added to yield 11? Should the string “5” be concatenated with the string “6” to yield “56”? Should the computer stop and report an error? Each language deals with this ambiguity in different ways. In Python, the above line would be an error. You’d have to explicitly tell the computer which interpretation you wanted. In Java the “6” would be turned into a string, resulting in the string “56”. In Perl, PHP, and Visual Basic the string concatenation operator is not plus, it’s period for Perl and PHP and ampersand in Visual Basic, so it’s always clear what needs to be done based on which operator is used. (That’s one advantage of using different operators for different things.)

To give you another example of how syntax can vary across different languages, here’s the way you add two numbers in our Python-like language:

5 + 6

And here’s the way you do it in the Lisp programming language:

(+ 5 6)

Notice two differences: the whole thing must be surrounded by parentheses; and the plus sign comes first. Again, separate in your mind the concept of addition from the specific syntax for addition used by the language you’re learning.