CS 403 Class Notes - Spring 2002: Iteration Statements and Exception Handling, Study notes of Programming Languages

The class notes for cs 403, a computer science course taught at a university during the spring 2002 semester. The notes cover the topics of iteration statements and exception handling. The notes include examples, explanations, and assignments.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-rgj
koofers-user-rgj 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4

Partial preview of the text

Download CS 403 Class Notes - Spring 2002: Iteration Statements and Exception Handling and more Study notes Programming Languages in PDF only on Docsity!

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- 4 9, 122- 23, 281- 8 4, 343- 45, 376- 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 originally 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

Side Effects in Expressions

  • Use these slide rather than previous ones on the

subject

  • Several possibilities 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 ++
  • Suppose 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 for 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 orders.
    • Terms:
      • (1) (a 1 ++) (where a 1 is the first a)
      • (2) (a 2 ++) (where a 2 is the second a)
      • (3) (1) * 3
      • (4) (2) * 5
      • (5) (3) + (4)
    • Possible evaluation orders that could result in different 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”

Iterative Statements

• Two general categories:

– Counter-controlled

– Boolean-controlled

  • Fixed exit
  • User-controlled exit

Spring 2002 CS 403 Class Notes (^) Page 7

Counter-Controlled Loops

• Example:

for i := 1 to 100 do

sum := sum + a[i];

• Terminology:

– Loop variable: i in this case

– Loop parameters: Starting and ending values for i

Spring 2002 CS 403 Class Notes (^) Page 8

Design Issues

  1. What is the type and scope of the loop var? Š Should it be a discrete type? Š Should it be restricted to integer? Š Should it be local to the loop?
  2. What is the value of the loop var at loop termination? Š Upper bound parameter? Š Upper bound parameter + 1? Š Undefined? Š Note: If scope restricted to loop, then this question goes away (which could be good).
  3. Should it be legal for the loop var or loop parameters to be changed in the loop body, and if so, does the change affect loop control? Š If you allow the loop var to be changed, then the number of iterations gets ambiguous
  4. Should the loop parameters be evaluated only once, or once for every iteration? Š If the loop changes the parameters, then if they are evaluated on every iteration, the loop itself could change the number of iterations Š This could make the code harder to verify

Counter-Controlled Loops

  • FORTRAN 77

S yntax: DO label var = start, finish [, stepsize]

  • Stepsize can be any value but zero
  • Parameters can be expressions
  • Design choices:
  1. Loop var can be INTEGER, REAL, or DOUBLE
  2. Loop var always has its last value
  3. The loop var cannot be changed in the loop, but the parameters can; because they are evaluated only once, it does not affect loop control
  4. Loop parameters are evaluated only once

Spring 2002 CS 403 Class Notes (^) Page 10

Counter-Controlled Loops

• Pascal

Syntax:

for variable := initial (to | downto) final do

statement

• Design Choices:

1. Loop var must be an ordinal type of usual scope

2. After normal termination, loop var is undefined

3. The loop var cannot be changed in the loop; the loop

parameters can be changed, but they are evaluated

just once, so it does not affect loop control

4. Loop variable evaluated just once

Spring 2002 CS 403 Class Notes (^) Page 11

Counter-Controlled Loops

• Ada

for var in [reverse ] discrete_range loop

end loop

Š Design choices:

  1. Type of the loop var is that of the discrete range; its scope is the loop body (it is implicitly declared)
  2. The loop var does not exist outside the loop.
  3. The loop var cannot be changed in the loop, but the discrete range can; it does not affect loop control.
  4. The discrete range is evaluated just once

Counter-Controlled Loops

  • C++ syntax:

for ([expr_1] ; [expr_2] ; [expr_3]) statement

  • The expressions can be whole statements, or even statement

sequences, with the statements separated by commas

e.g., for (i = 0, j = 10; j == i; i++) ...

  • If the second expression is absent, it is an infinite loop.
  • There is no notion of “loop variable”, and expressions 2 and 3

might have nothing to do with incrementing and terminating

based on a counter:

– for (cin >> x; x =- 1 ; y += x) is legal, but doesn’t fit

the basic paradigm of counter- controlled loops

Spring 2002 CS 403 Class Notes (^) Page 19

User-Located Loop Control Mechanisms

3. FORTRAN 90 - EXIT

Unconditional; for any loop, any number of levels

FORTRAN 90 also has CYCLE, which has the same semantics as C's continue

Spring 2002 CS 403 Class Notes (^) Page 20

Unconditional Branching

  • The “Goto” statement
  • Problem: readability
  • Some languages do not have them:e.g., Modula-2 and

Java

  • Label forms:
    1. Unsigned int constants: Pascal (with colon) FORTRAN (no colon)
    2. Identifiers with colons: ALGOL 60, C
    3. Identifiers in << ... >>: Ada

Spring 2002 CS 403 Class Notes (^) Page 21

Exception Handling

• Exception:

  • User defined
  • Language defined

• An exception can be thrown by:

  • Standard language routines
  • User routines

• User- written exception handlers can catch either

type of exception

• Allows:

  • Graceful exit from segments of code which shouldn’t be

executed in the face of an exception.

  • Programmer controlled handling of exceptions raised by

standard library routines

Spring 2002 CS 403 Class Notes (^) Page 22

Exception Handling: Simplest Case

int main(){ cout << “Enter positive integer” << endl; try{ double x; cin >> x; if (x < 0) throw(x); else sqrt(x);} catch(double x) {cerr << “x = “ << x << endl; abort();} }

  • Involves throwing a user exception
  • Slightly better than just calling a function:
    • If no abort, return to the next statement after the try block, not to the statement after the throw

Spring 2002 CS 403 Class Notes (^) Page 23

Exception Handling: Library Raised

Exception

#include int main(){ int *p, n; try{ while (true){ cout << “Enter allocation request: “ << endl; cin >> n; p = new int[n]; } } catch(bad_alloc) {cerr << “bad_alloc” << endl;} }

  • Request for 1,000,000,000 integers results in a built-in exception: “bad_alloc”
  • This request is caught and the program terminates more gracefully than with a segmentation fault, etc.