Datasheet
Chapter 1: A Quick Introduction to Programming
19
When you have a situation where more than one operation occurs in an expression, the operations are
normally performed from left to right. However, there are several rules.
Operators from the arithmetic group are evaluated first, then concatenation, comparison, and finally logical
operators. This is the set order in which operations occur (operators in brackets have the same precedence):
❑ ∩, −, (*, /), \, Mod, (+, −)
❑ &
❑ =, <>, <, >, <=, >=, Is
❑ Not, And, Or, Xor, Eqv, Imp
This order can be overridden by using parentheses. Operations in parentheses are evaluated before
operations outside the parentheses, but inside the parentheses, the normal precedence rules still apply.
Take a look at the following two statements:
A = 5 + 6 * 7 + 8
A = (5 + 6) * (7 + 8)
They look the same but they’re not. According to operator precedence, multiplication is performed
before addition, so the top line gives
A the value 55 ( 6 * 7 = 42 + 5 + 8 = 55 ). By adding parenthe-
ses, you force the additions to be evaluated first and
A becomes equal to 165.
Organizing and Reusing Code
So far, the scripts you’ve worked with have been fairly simple in structure. The code has been all
together in one unit. You haven’t done anything all that complicated, so it’s easy to see all the code in
just a few lines. The execution of the code is easy to follow because it starts at the top of the file, with
the first line, and then continues downward until it reaches the last line. Sometimes, at certain points,
choices redirect the code using branching, or sections of code are repeated using loops.
However, when you come to writing a script that actually does something useful, your code is likely to
get more complex. As you add more code to the script, it becomes harder to read in one chunk. If you
print it on paper, your scripts will undoubtedly stretch across multiple pages. As the code becomes more
complex, it’s easier for bugs and errors to creep in, and the poor layout of the code will make these
harder to find and fix. The most common technique programmers use to manage complexity is called
modularization . This is a big, fancy word, but the concept behind it is really quite simple.
This section defines some terminology used when organizing and reusing code, and then discusses how
to write your own procedures by turning code into a function. You then learn a few advantages of
having procedures.
c01.indd 19c01.indd 19 8/27/07 7:45:24 PM8/27/07 7:45:24 PM