
1
Spring 2002 CS 403 Class Notes Page 1
Lecture 20 – Tuesday, March 18
• Today
– Iteration statements
– Exception handling
• Reading Assignments For Next Class
– pp. 45-49, 1 22- 23, 281-84, 343- 45, 37 6-82
• Assignments
– Assignment #5 due one week from today
Spring 2002 CS 403 Class Notes Page 2
What About ++?
• Consider: (a++ * 4 – c)
– Assume a = 2, c = 1
– By precedence, we get:
• ((a++) * 4) – c
– Result of a++ = 2
• (2 * 4) – 1 = 7
– After a is used it becomes 3.
• Consider: (++a * 4 – c)
– a original ly 2; a set to 3 before it is used
– (3 * 4) – 1 = 11
• ++ induces a “side effect” (makes a change) on the
term of the expression to which it is applied
Spring 2002 CS 403 Class Notes Page 3
Side Effects in Expressions
• Use these s lide rather than previous ones on the
subject
• Several p ossibilities as to what might happen with ++
• Example: (a++ * 3 + a)
– Precedence rules: ((a++) * 3) + a)
– “Undefined evaluation order” extends to include evaluating
individual variables.
•Strategy 1: All instances of “a” could be loaded at the
beginning
•Strategy 2: The second instance of “a” could be loaded
after the ++
• Suppos e a is 2:
– With Strategy 1: Answer is 8.
– With Strategy 2: Answer is 9.
Spring 2002 CS 403 Class Notes Page 4
A Second Case
• Another ++ case: a++ * 3 + a++ * 5
– Precedence rules: ((a++) * 3) + ((a++) * 5)
– No way to apply associativity
– By previous slide, two general strategies f or loading the a’s:
• Strategy 1: At the beginning at the same time
• Strategy 2: Serially (one after the other)
– Strategy 1: Assume a is 2 – answer is 16.
– Strategy 2: There are two possible evaluation order s.
– Terms:
•(1) (a1++) (where a1is the first a)
•(2) (a2++) (where a2is the second a)
•(3) (1) * 3
•(4) (2) * 5
•(5) (3) + (4)
– Possible evaluation orders that could result in dif ferent answers:
•1,2,3,4,5 (a = 2 – answer is 21)
•1,2,4,3,5 (a = 2 – answer is 19)
Spring 2002 CS 403 Class Notes Page 5
Control Flow Categories for Discussion
• Sequence (already done)
• Selection (already done)
• Iteration
• Procedures
• Recursion
• Need to define a preliminary term before launching
this discussion: “control structure”
Spring 2002 CS 403 Class Notes Page 6
Iterative Statements
• Two general categories:
– Counter-controlled
– Boolean-controlled
• Fixed exit
• User-controll ed exit