Tinkering with Perl

Jonathan's Corner (Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  24  25  26  27  28  29  30  31  32  33  Next  Skip Forward
Printer-Friendly Version

Arithmetic

Perl, like many languages, lets you do arithmetic with numbers. You can have a statement like

$Average = ($FirstNumber + $SecondNumber + $ThirdNumber) / 3;

and the computer will do the arithmetic for you. It will add the three numbers, and divide the result by three. The result is stored in Average.

You may have noticed that I had parentheses -- why are they necessary? This has to do with something called order of operations. Let's say we want to figure out what 3 * 4 + 5 equals. Well, what do you do first -- multiply 3 by four, or add 4 and 5? If you multiply first, then 3 * 4 = 12, so you have 12 + 5, or 17. So the expression equals 17. But what if you add first? Then 4 + 5 = 9, and 3 * 9 = 27, so we have 27. The number you get depends what you do first.

Parentheses are a way to tell the computer what to do first. Everything inside a pair of parentheses is calculated before everything outside of the pair of parentheses. Everything on an inside pair of parentheses is calculated before things on an outside pair of parentheses. So, for example, if we have (((3 * 4) + 6) / 9), that means that we first multiply 3 * 4 = 12, to get ((12 + 6) / 9); then we add 12 + 6 = 18, so we get (18 / 9), and then we divide 18 / 9 = 2, so we get a result of 2.

At least for now, you should always use parentheses to tell the computer what it should do first. Use parentheses, so that the computer knows exactly what order you want it to do things in.

Two notes:

First, if you ask it to do something that's going to give a fraction (like "What is five divided by three?"), it will give a decimal for an answer. Usually, if there is a decimal involved in a calculation, the result will be a decimal. (With decimals, an answer will usually be a little more or a little less than it should be. That is a kind of error that happens with computers.)

Second, you can't divide by zero. If you try to divide by zero, your program will stop running. It is good practice, before dividing by a variable, to make sure that it is not zero (see if-then).

It is generally good, when doing arithmetic calculations, to break them into as many small steps as possible. It is better to have several simple calculations than one really long and confusing one.

See also:

Scalars - Assignment of scalars - Conditional clauses - If-then

Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.

Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)

Read more...

Top

Jonathan's Corner (Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  24  25  26  27  28  29  30  31  32  33  Next  Skip Forward
Printer-Friendly Version