Lecture #9: More Operators and Expressions

 

Bob Pitha

bpitha@cs.ucsd.edu

http://www-cse.ucsd.edu/classes/fa99/cse5a

quiz issues

•      On Friday in class

–   it will not be a Big Exam

•      What will it cover?

–   everything we’ve talked about!

•      Please be on time

•      Bring a #2 pencil

question

•      How many of you would use a copy of Visual C++?

–   you’d 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.

–   Let’s ignore all but the “divisible by 4” rule…

Text Box: if (year % 4 == 0)
	printf (“%d is a leap year\n”, year);
else
	printf (“%d is not a leap year\n”, year);
 


increment/decrement operators

Text Box: ++   --
 


•      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

–   that’s 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 doesn’t constrain what must happen

–   So it depends on the machine/compiler you are using

•      Overflow for unsigned integer types “wraps around”:

Text Box: unsigned i = 0;
i--;
 


    i is now 4 billion and something.

precedence and associativity


 


precedence and associativity


 


•      This means the following expression:

Text Box: i - j++
 


•      is interpreted as:

Text Box: i – (j++)
 


•      and not as:

Text Box: (i – j)++
 


increment example

Text Box: int numCalls = 0;
int count (void)
{
   return numCalls++;
}
 


•      This is a function that returns a number

–   every time it’s 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:

Text Box: i = j = k = 37;
 


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

Text Box: i = j + 75;
 


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.

–   it’s 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:

Text Box: i < j + 3
 


    will be interpreted as:

Text Box: i < (j + 3)
 


    and not

Text Box: (i < j) + 3
 


relational operator issues

•      Be careful with mixed types

•      Example:

Text Box: unsigned i = 0;
if (i > -1) {
   …
 


–   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