UMBC CMSC201, Computer Science I, Fall 1994
Sections 0101, 0102 and Honors
Tuesday September 20, 1994
Assigned Reading: 2.4 - 2.5
Handouts (available on-line): none
Topics Covered:
- Expressions.
Rules for forming integer expressions.
Here's a copy of the operator
precedence and associativity table.
- A program
that uses expressions in unexpected ways and
a sample run.
- The modulo operator, %, can be used to calculate
the remainder of an integer divided by another integer.
(E.g., 7 % 4 is 3.) We can use the modulo operator
to modify the program that converts centimeters to
feet and inches.
Program and
sample run.
- Mixing types can lead to strange results.
Generally, if a binary operator is used on both
an integer and a double, the integer is converted
to a double and the result is a double. So, dividing
a 9.0 by 4 gives the double value 2.25. Dividing 9 by 4
gives the integer value 2. Here's a
program and
sample run that demonstrates
division and mixing types.
- Expressions that change the value of one or more variables
are said to have a side effect. For example, an expression that
uses the assignment operator has a side effect. Since side effects
can be unpredictable, expressions with side effects should be
avoided. The only exception is simple assignment statements.
Here's a program with unpredictable
side-effects. The sample runs
on different machines give different results. This is because C
does not explicitly say if the left subexpression or the right
subexpress of a binary operator should be evaluated first.
- Rules for naming variables.
- Discussed shorthand assignment operators: +=, -=, etc. We also
discussed the difference between ++x and x++.
Last Modified:
Thu Sep 22 17:22:46 EDT 1994
Richard Chang, chang@gl.umbc.edu