MIME-Version: 1.0 Server: CERN/3.0 Date: Sunday, 24-Nov-96 22:48:48 GMT Content-Type: text/html Content-Length: 17673 Last-Modified: Tuesday, 03-Sep-96 19:38:27 GMT
REVIEW ó previous lecture
problem, algorithm, program, language, computer,
program/data distinction, input/output, execution, trace
program structure, output statement, conditional
expression, operators, input function
CONCEPTS ó this lecture
expressions: arithmetic, relational, logical, conditional;
operator precedence and parenthesization; function application;
convergence & divergence, strict & non-strict evaluation,
programs; output; libraries
READING
Teitelbaum, Sections 2.1ñ2.7
Roberts, Section 2.1, Section 3.5
ï Familiar from grade school arithmetic.
(2 ¥
3) + (20
_ 5)
ï Consist of operators, e.g. +,
¥, _,
and operands, e.g., constants. An operator applied
to its operands is an application. Sub-expressions are
parenthesized to indicate structure.
ï Reduce to a single value by process
of evaluation.
(2 ¥
3) + (20 _
5) Þ
10
ï Evaluation of an expression can be traced.
(2 ¥ 3) + (20 _ 5)
6 + (20 _ 5)
6 + 4
10
The process used in evaluation and tracing is expression
simplification.
Inside-Out Simplification: To evaluate expression e, repeatedly replace an innermost application e' within e by its value using the appropriate rule of arithmetic. Stop when e has been reduced to a single value.
Note: left-to-right evaluation not required.
Binary Operators
Operator | + | - | * | / | % |
Meaning | + | - | ¥ | _ | remainder |
Examples:
6 + 1 Þ
77 - 2 Þ
57 * 2 Þ
1414 / 3 Þ
414 % 3 Þ
2
Unary Operators
Operator | + | - |
Meaning | identity | negation |
Examples:
-(2 + 3) Þ
-5+(2 * 3) Þ
6
Relational Operators
Operator | < | <= | > | >= | == | != |
Meaning | < | £ | > | ³ | = | ¹ |
ï In C, relational operations are just arithmetic operations:
they yield integer values.
ï C interprets 0 as logical "false". All other
integers are treated as logical "true". In particular,
relational operators use 1 to mean "true".
ï Remember that the equality relation is ==.
Using =
means something else in C; never use =
in an expression.
Examples:
5 == 5 Þ
15 < 5 Þ
04 < 5 Þ
15 <= 5 Þ
1
ï Parentheses can be used to group operators
explicitly.
ï In the absence of parentheses, operator groupings
are determined by operator precedence.
High | ... | ... | ... | Low |
+ - (unary) | * / % | + - (binary) | < <=
> >= | == != |
ï An operator with higher precedence groups "more tightly"
than an operator of lower precedence.
ï Operators of same precedence group to the left.
Example:
1 + 2 * 3 + 4 < - 5 - 6 - 7
groups as
((1 + (2 * 3)) + 4) < (((-5)
- 6) - 7)
ï You can always add parentheses to make the
intended meaning clear.
Example: 1+2*3 is the same as 1+(2*3) as far as
the computer is concerned.
ï But, to realize that the value of this expression
is 7 (and not 9) a human needs to remember precedence order.
ï Programs are written to be executed by computers
and to be read by humans. In many respects, the latter is the
most important part.
ï My personal rule is to always use parentheses if the meaning of the expression is at all ambiguous. (Extra parentheses (never) hurt.)
name(arguments)
ï arguments is a list of expressions
separated by commas.
ï The value of this expression is obtained by
applying function name to the arguments.
ï The number of arguments in the argument list
must match the arity of the defined function name.
There are no functions built into C. Functions are
defined externally by libraries.
Examples:
abs(-3)sqr(3 + 4)GetInteger()hypotenuse(3,
4)
condition ? true-exp : false-exp
ï The value of a conditional expression is the
value of false-exp if condition evaluates to 0,
or the value of true-exp otherwise.
ï Since C relational operators use values 1
and 0 for true and false, this allows a choice of value based
on the outcome of a relational test. (But condition is
not limited to relations.)
Examples:
(1 < 2) ? 1 : 2 Þ
1(2 < 1) ? 1 : 2 Þ
21 + 1 ? 15 : 14 Þ
15
ï First the condition is evaluated, and
then either the true-exp or the false-exp is evaluated,
but not both.
ï Evaluation of an expression diverges
if the result of an application is undefined. Evaluation converges
if it doesn't diverge.
Example:
10 / 0 Þ
diverges
ï An operator is strict if it evaluates
all of its operands. It is non-strict if it doesn't necessarily
evaluate all of its operands.
Example:
+, -, *, / are
strict:
e.g., 0
* (1 / 0) Þ
diverges
conditional expressions are
non-strict:
e.g., 1 ? 2 : (3 / 0) Þ 2
ï Logical operators are used to combine the
logical values, true and false, in various ways.
and | or | not | |||||||
F | F | F | |||||||
T | T | T |
ï In C, logical values are represented by 0 and 1, so the
logical operators are also arithmetic operators.
Operator | && | || | ! |
Meaning | and | or | not |
Examples:
0 && 0 Þ 0 | 0 || 0 Þ 0 | ! 0 Þ 1 |
0 && 1 Þ 0 | 0 || 1 Þ 1 | ! 1 Þ 0 |
1 && 0 Þ 0 | 1 || 0 Þ 1 | ! 2 Þ 0 |
1 && 1 Þ 1 | 1 || 1 Þ 1 |
0 && 1/0 Þ 0 |
void main(void) { statements }
ï All programs have this general form.
ï statements are commands that direct
the computer to do something.
ï The statements are executed from left to right
in order.
ï The program stops after executing the last
statement.
printf( format, expressions );
ï This is the only statement you need for now.
ï Prints output to a destination, usually a
window on the screen.
ï format is a quoted string giving the
desired form of the output.
ï expressions are the things to be printed.
ï If there are no expressions, format
is just a constant string to be output.