CIS 400 Lecture 9
Functional
programming, (skipping chapter 5 for now, will come back to it)
Sequence
control
·
Used
in expressions, (e.g. Precedence rules, parenthesis)
·
Between
statements or blocks, (e.g. Iteration and conditionals)
·
Between
sub-programs, (e.g. Calls)
Control
sequences
·
Implied,
(physical statement order)
·
Explicit,
(parenthesis or goto's)
Example:
Root
= -b ± (b2-4ac)1/2
2a
In
Fortran:
Root = ( -b + Sqrt ( b**2 - 4*a*c))/2*a
In
some instances, parenthesis are essential, in other they are not.
(a
+ b)*(c - a) in-fix notation
* +
a b - c a pre-fix notation
a b
+ c a - * post-fix notation
What
is the difference between unary and binary minus?
(i.e.
how does "-b" and "b2-4ac" in the quadratic formula
differ in terms of language definition?)
Tree
representation: issues
-a
* b + c in-fix notation
·
Binary
vs. unary operator confusion
·
Precedence
rules
·
Associativity
v
left to right
v
right
to left, (eliminates the need for parenthesis for the "2a" in the
denominator of the quadratic)
·
Operators
with varying number of operands, ( i.e. (+ 2 3 4))
Problem
areas:
·
Uniform
evaluation-once a tree is formed, all operands are treated equally
·
Side
effects:
·
Error
conditions: arithmetic over- or under-flow, (i.e. 4/ (200 * 10,000,000))
·
Short
circuited boolean evaluation:
(a = 0) OR (b / a > c) in short circuit
evaluation, the "OR" is evaluated until a "true" is found.
With full boolean evaluation, the "b/a",
where a = 0, is evaluated first. This results in a run-time error.
With an "and", the first "false"
stops evaluation.
Statement
level control structures:
·
Composition,
(sequence)
Example:
Block statements
·
Alternating
(conditional statements)
v
If-then/else
or case, (switch)
"dangling else" issue:
if x = 3
then
y = 5
if x = 5 then
y = 6
else y = 7
// WHICH "IF" GETS THE "ELSE"?
in C++ {} is used to define where the else belongs
v
If-then/else
or case, (switch) continued
Case
design issues:
·
What
type of selector expression?
·
What
types of case labels?
·
Can
you branch to case labels from outside?
·
Mutually
exclusive labels?
·
Exhaustive
label case coverage? ("default", etc.)
Iteration
issues:
In
COBOL;
Perform
{body} k times
How
often is k evaluated?
If
k is re-evaluated, when?
Can
k < 0?
·
When
is termination test made?
·
When
are the loop expression variables evaluated?
(Pre
and post test loops)
Counter
incrementing loop:
do i = start to end by increment until (cond)
begin
(body)
end;
Is
this pre- or post-test evaluation?
In
Fortran 4 it was post-test!!
In
Fortran 77 it was pre-test!!
When
is i evaluated?
Can
"start" or "end" be altered?