CS 204: Math Expressions
Introduction
This
course normally has a math prerequisite of high school level algebra. For many of us, however, it has been a long
time since we took that course. This
chapter is to refresh our basic math terminology so that we can read the book
without needing to consult a math text.
Key Terms:
First,
we will present a list of math terms that will be used in this class.
Constant
A
constant has a value that does not
change. It is known from the beginning,
and can’t be altered. The number 589,
for example, is an integer constant. So
is the value: 98.6. Most constants are
numbers.
Variable
A
variable is something that can be
changed. It is a like a box, a spot in
the computer’s memory, in which we can store a constant value. Variables hold constant values, but they
aren’t constants, because their meaning might change during the progress of a
program. Variables have names, such as
X, COUNT, MAXIMUM or VALUE. You, as the
programmer, will create variables, give them names,
and decide what to do with them.
Expression
An
expression in math terms is a legal
sequence made up of operators and operands, Such as:
A
+ B
This
is a very simple expression. It has two
values known as A and B and adds them together using the operator + which
indicates addition shall be performed on these two values.
Operator
An
operator is part of an expression
that is like the verb in a sentence. It
indicates what type of action will take place when the expression is evaluated.
Operators
and their meanings:
+ addition
- subtraction
* multiplication
/ division
Operand
Operands
are like the nouns in a sentence. They
are the objects upon which the operators will act. Every operand in a given
expression can be either a variable or a constant. For example the expression:
A
- 5 + COUNT
Would
cause the constant value of 5 to be subtracted from the variable A, and then
the value of the variable COUNT would be added to the result of the
subtraction. So, first we would take the
number in the variable A, subtract 5 from it, then add
the value in the variable COUNT.
Evaluate
One
of the keys to understanding expressions is knowing
how to evaluate them. This means you must know how to calculate the
correct answer from a given expression.
When programming, the computer will do this for you, but you must give
it the correct expression to evaluate or it will not give you the right answer.
In
the chart below, for example, if we assume the value of A is 5 and the value of
B is 6, each of the following expressions evaluate
to the values on the right.
Expression: Evaluation:
5 + A 10
6 + B 12
A + B 11
B – A 1
B + A – 10 1
Operator Precedence
Making
things slightly more complex, not all operators are born equal. Some operators are more important than others,
and are therefore evaluated first, regardless of order. Understanding these rules and how they work
is critical for programmers, as they must create correct formulas for their
programs to work properly.
Normally,
if no rules of operator precedence
apply, an expression is evaluated from left to right. For example:
5
+ 6 - 7
When
evaluated, first we add the 5 and the 6, then subtract
the 7, getting 4.
However,
multiplication and division have a higher order of operator precedence than
addition or subtraction. This means that
those operations are evaluated first, regardless of order.
5
+ 6 * 2 - 7
In
the above example, you first would multiply the 6 and the 2. Then you would add the 5, then
subtract the 7. The final answer then
would be 10. If you were to do it left
to right, without applying operator precedence, you would add 5 and 6, getting
11, then multiply the 11 by 2, getting 22, then
subtracting the 7, resulting in 15.
Unfortunately, this would be quite wrong. The correct answer is 10.
Operator Precedence Table:
( ) Parentheses
(used to override
operator precedence)
- Unary
minus, (as with negative numbers)
^ Exponentiation
(Basic only)
*, /, % (MOD in Basic) Multiplication,
division, modulo
-, + Subtraction,
addition
When
evaluating a complex expression, we normally use parentheses to make sure that
things happen the way that we want. For
example:
2
* (4 + 8 - 2) / 5
would be evaluated as follows: First, we would evaluate
the sub-expression in the parentheses. 4
+ 8 - 2 gives us 10. We would then
evaluate the rest of the expression in left to right order, since
multiplication and division are on the same level of the operator precedence
chart. Accordingly, we would multiply 2
by 10, getting a value of 20, then divide 20 by 5, resulting in a final value
of 4.
When
your expressions become large and complex, it is useful to break down the
evaluations into a series of steps. To
decide what step to take next, you must consult the operator precedence chart.
Examples:
Assume:
A = 3
Expression: 6 / 2 + A - 3
Step 1: 6
/ 2 + 3 - 3
Step 2: 3 +
3 – 3 Answer:
3
Expression: 7 + (5 * A - 2) - 5
Step 1: 7 + (5 * 3 - 2) - 5
Step 2: 7
+ (15 - 2 ) -
5
Step 3: 7
+ (13) - 5 Answer:
15
Expression: B * 5 - 5
Step 1: 5
* 5 - 5
Step 2: 25
- 5 Answer:
20
Expression: A * (4 - 2)
Step 1: 3
* (4 - 2)
Step 2: 3
* 2
Answer:
6
Expression: (4 * 4) / ( 2 - -2 )
Step 1: (16)
/ (2 + 2 )
Step 2: 16
/ (4) Answer:
4
Expression: (A + A + 4) / 5 * (B - A)
Step 1: (3
+ 3 + 4) / 5 * (5 - 3)
Step 2:
10 / 5 *
2
Step 3: 2 *
2 Answer:
4
Assignments are computer operations that put values into variables. The values are generated by evaluating an
expression. The value generated is then
placed into a variable for later use.
Most math operations performed by a computer involve assignments.
In both C and Basic, as well as most other
computer languages, the assignment is a simple but powerful tool. It is very important that beginning
programmers thoroughly understand assignments, as they can be confusing to
non-programmers, and they will be used in practically every program you will
ever write.
The basic structure of an assignment is to place a variable on the left
side, then an equals sign, then an expression on the
right side. The expression can be any
legal expression of the type explained in the preceding sections.
Example:
A = 12
B = 18
The two assignments above
would work in either C or Basic. These
two computer operations would place the value of 12 into a variable called A, and the value of 18 in a variable called B.
It is important to realize that assignments are not the same as equality
operators. That is to say, before the
above two lines were exectuted, A was not necessarily
12, and B was not necessarily 18. In
mathematics, we would call them equations,
and they would mean than A was a 12 and always would be, and that B was an 18
and always would be. In computer
science, the above lines change what
was in the variables, rather than simply stating what they are.
This allows us to do things you could never do in mathematics.
Example:
A = 12
B = 18
A = B
How is this legal? Let’s keep in
mind what is happening. In each of the
three assignments above, the value on the right is moving into, and erasing,
the value of the variable on the left.
After these three operations, both A and B contain the value of 18. The value of 12 was in A briefly, but it was
erased by the value of 18 when it was moved from B into A. In a computer program, a series of
expressions like those above are not related to each other. They are executed line by line, from the top
to the bottom. They operate by moving
data around, not by indicating a previous relationship between the
expressions.