Expressions, Precedence and Associtivity
Document Sample


Intro to Computer Science SI204
Expressions, Precedence and Associtivity
What happens when an expression like 2 + 3 * 5 is
evaluated? Do I get 17 or 25? Well, your math classes
should've taught you that 17 is the answer, and indeed
that's true in C++ as well.
When you have two different operators in an expression and
parentheses are not used to tell you which operation is
performed first, the relative precedence of operators is what
determines which operation is performed first.
Since * has a higher precedence than +, the expression
2 + 3 * 5 is evaluated like 2 + (3 * 5). But what about
when both operators are the same, or both have the
same precedence? What happens with a * b * c?
1
Intro to Computer Science SI204
Associtivity
When you have two identical operators in an
expression (or two different operators with the same
precedence) and parentheses are not used to tell which
operation is performed first, the associtivity of the
operator(s) is what determines which operation is
performed first.
The associtivity of * and / (which both have the same
precedence) is left-to-right, so a * b * c is evaluated as
(a * b) * c. This can matter in C++. (For example, what
does 3 / 4 * 1.5 evaluate to?)
Always use parentheses rather than relying on
subtle precedence and associtivity rules
2
Intro to Computer Science SI204
Operator Precedence Table
3
Intro to Computer Science SI204
Decisions, Decisions … if Statements
The ability to make decisions and react to different input is a
key part the power of computers. For example, we would
certainly expect it to be possible to write a program that reads
in a number and writes "even" if the number is even, and "odd"
if the number is odd. In C++ (as in English!) "if" is the key to
expressing this.
4
Intro to Computer Science SI204
Example: Even or Odd?
A number is even if 2
divides it evenly, i.e. if
its remainder when
divided by 2 is 0. So for
k to be even, k % 2 must
be zero.
The "= =" operator
returns a bool that is
true if the left and
right-hand expressions
are equal, and false
otherwise.
Thus, (k % 2) == 0 is the
test condition we need.
5
Intro to Computer Science SI204
Scope
We also might consider solving the even/odd problem
by assigning a variable of type string the value "even"
if k is even and "odd" otherwise.
Then, after the if-statment,
we'll do the printing.
We might implement it
like this ===========>
However, the compiler will
complain about an
“undeclared identifier s”
when it tries to compile
cout << s << endl
6
Intro to Computer Science SI204
Scope
To fix up this version of our even/odd program, we
simply need to move the declaration of s outside of
the if/else-blocks so that
its scope extends to
the cout statement.
7
Intro to Computer Science SI204
Relational Operators
Relational operators make comparisons of their left and right-
hand arguments, and return bool values accordingly.
The relational operators are:
= = (equal)
!= (not equal)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
Since Relational operators have lower precedence than the
arithmetic operators, things like 2*k > k + 5 evaluate the
arithetic expressions on the left and right, and then apply the
">" operator to compare the two values.
This means, for example, that instead of writing ((k % 2) ==
0) we could write (k % 2 == 0) and get the same result.
8
Intro to Computer Science SI204
Blocks
Code in between {}'s
forms a block.
Blocks allow you to
declare variables,
read input, make
assignments to
variables ... But
the scope of what
you declare inside
a block is the block
itself.
9
Intro to Computer Science SI204
if without else
Sometimes you have a condition which, if it's true,
should cause you to do some extra work, but which, if
it's false, should have no effect on the program. In
this case, we can have just an if without the else part
10
Intro to Computer Science SI204
ICE: Remainders
Write a program that reads in two integers from the
user, and prints out the remainder when the larger is
divided by the smaller.
Note that we can use the following output line, but we
1st need to determine which input value is larger so it
can be loaded into the variable “a” and put the smaller
value in “b”.
cout << "The remainder when " << a << " is divided by “
<< b << " is " << a % b << endl;
11
Related docs
Get documents about "