This preview shows pages 1–3. Sign up to view the full content.
Arithmetic Expressions in C
Arithmetic Expressions consist of numeric literals, arithmetic
operators, and numeric variables. They simplify to a single
value, when evaluated. Here is an example of an arithmetic
expression with no variables:
3.14*10*10
This expression evaluates to 314, the approximate area of a
circle with radius 10. Similarly, the expression
3.14*radius*radius
would also evaluate to 314, if the variable radius stored the
value 10.
You should be fairly familiar with the operators + ,  , * and /.
Here are a couple expressions for you all to evaluate that use
these operators:
Expression
Value
3 + 7  12
6*4/8
10*(12  4)
Notice the parentheses in the last expression helps dictate
which order to evaluate the expression. For the first two
expressions, you simply evaluate the expressions from left to
right.
But, the computer doesn't ALWAYS evaluate expressions from
left to right. Consider the following expression:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
If evaluated from left to right, this would equal (3+4)*5 = 35
BUT, multiplication and division have a higher order of
precedence than addition and subtraction. What this means is
that in an arithmetic expression, you should first run through
it left to right, only performing the multiplications and
divisions. After doing this, process the expression again from
left to right, doing all the additions and subtractions. So, 3+4*5
first evaluates to 3+20 which then evaluates to 23.
Consider this expression:
3 + 4*5  6/3*4/8 + 2*6  4*3*2
First go through and do all the multiplications and divisions:
3 + 20  1 + 12  24
Now, do all the additions and subtractions, left to right:
10
If you do NOT want an expression to be evaluated in this
manner, you can simply add parentheses (which have the
highest precendence) to signify which computations should be
done first. (This is how we compute the subtraction first in
10*(12  4).)
So, for right now, our precedence chart has three levels:
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '08
 Guha

Click to edit the document details