Lecture #9: More Operators and Expressions
Bob
Pitha
quiz issues
On
Friday in class
it will not
be a Big Exam
What
will it cover?
everything
weve talked about!
Please
be on time
Bring
a #2 pencil
question
How
many of you would use a copy of Visual C++?
youd have
to have a PC running Windows
arithmetic operator example
Leap
year determination
A year is a
leap year if it is divisible by 4 but not by 100, except for years divisible by
400.
Lets ignore
all but the divisible by 4 rule

increment/decrement operators

Unusual
operators: they change their operand (side effect) as well as return a value
Single
operand
2
flavors: pre- and post-
preincrement/predecrement
value of
expression is modified value
postincrement/postdecrement
value of
expression is value before modification
Can
be used as a statement by itself, to just modify the value
single operand
This
is our first example of a unary operator
thats just
a fancy name for an operator that takes only one operand
preincrement/predecrement
Operator comes before value: ++i
Value
of expression is incremented/decremented value
Example:
int i = 12;
int j;
j = ++i;
Final value of i is 13, j is 13
computer simulation: preincrement
postincrement/postdecrement
Operator
comes after value: i++
Value
of expression is the original value, before it was modified.
Example:
int
i = 12;
int
j;
j
= i++;
Final value of i is 13, j is 12
computer simulation: postincrement
increment/decrement operators (cont.)
Overflow
for signed integer types is undefined
overflow
means going beyond the range the type can hold
undefined
means the ANSI standard doesnt constrain what must happen
So it
depends on the machine/compiler you are using
Overflow
for unsigned integer types wraps around:

i is now 4 billion and something.
precedence and associativity

precedence and associativity

This
means the following expression:

is
interpreted as:

and
not as:

increment example

This
is a function that returns a number
every time
its called, the number is one greater than the last time
simple assignment operator
=
Sets
the value of the lefthand operand to that of the right (after conversions)
Left
operand must be an lvalue
Value
of expression is the new value of the lefthand operand
i.e., the
converted value
value of assignment expression
What
does this mean?
remember
that expressions can have both a value and an effect
with the =
operator, the effect is the primary reason for its existence
it sets the
value of an object
however, the
whole expression has a value
which is the
new value of the lefthand operand
this means
the following statement is legal:

compound assignment operators
+= -=
*= /= %=
Binary
operators
a op= b is equivalent to a = a op b

Operand
types are restricted as for the normal arithmetic operators
precedence and associativity
The
assignment operators are almost the lowest precedence
we want to
evaluate everything to the right of the = before assigning the value to the
lefthand operand

assignment Issues
Right
associativity means that
a
= b = c = d + 7
and
x
*= y = z
relational operators
> >=
< <=
All
binary operators
take two
operands
All
yield an int, with a value of 1 or 0 (true/false)
in C, there
is no true/false value.
its always
0 or not-0
relational operators
These
are used to compare values, usually when making decisions
since they
yield a value, you could use them in an equation
but that
would be weird

precedence and associativity
So
something like this:

will be interpreted as:

and not

relational operator issues
Be
careful with mixed types
Example:

remember the two operands must be of the same type.
C converts
the 1 to an unsigned value
which is
therefore a very big number
and
(counterintuitively) the expression is actually false