CS 403 Class Notes: Iteration Statements, Go to Statements, and Exception Handling, Study notes of Programming Languages

A set of class notes from a spring 2002 computer science 403 class. The notes cover the topics of iteration statements, go to statements, and exception handling. The notes include examples and design issues for counter-controlled loops in various programming languages, as well as user-located loop control mechanisms and unconditional branching. The document also discusses exception handling and its benefits.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-ytm
koofers-user-ytm 🇺🇸

4

(1)

10 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Spring 2002 CS 403 Class Notes Page 1
Lecture 21 – Thursday, March 21 (V2)
Today
Iteration statements
Go to statements
Exception handling
Reading Assignments For Next Class
Finish reading Chapter 6
Chapter 8.5 (exception handling)
Assignments
Assignment #5 due Thursday April 4th
•Exams:
Exam 2: Moved from March 21st to Tuesday, April 9th
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download CS 403 Class Notes: Iteration Statements, Go to Statements, and Exception Handling and more Study notes Programming Languages in PDF only on Docsity!

  • Lecture 21 – Thursday, March 21 (V2)
  • Exception handling– Go to statements– Iteration statementsToday
  • Chapter 8.5 (exception handling)– Finish reading Chapter 6Reading Assignments For Next Class
  • Assignment #5 due Thursday April 4Assignments

th

  • Exam 2: Moved from March 21Exams:

st to Tuesday, April 9^

th

• Two general categories: Iterative Statements

  • Boolean-controlled– Counter-controlled
  • User-controlled exit• Fixed exit

1. What is the type and scope of the loop var? Design Issues

 Should it be a discrete type?

 Should it be restricted to integer?

 Should it be local to the loop?

  1. What is the value of the loop var at loop termination?

 Upper bound parameter?

 Upper bound parameter + 1?

 Undefined?

 good).Note: If scope restricted to loop, then this question goes away (which could be

  1. 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? ambiguousIf you allow the loop var to be changed, then the number of iterations gets

  1. Should the loop parameters be evaluated only once, or once for every

 iteration? iteration, the loop itself could change the number of iterationsIf the loop changes the parameters, then if they are evaluated on every

 This could make the code harder to verify

• Counter-Controlled Loops

S FORTRAN 77

yntax:

(^) DO (^) label var = start, finish [, stepsize]

Stepsize can be any value but zero

Parameters can be expressions

  1. Design choices:

Loop var can be

(^) INTEGER

, REAL

, or (^) DOUBLE

Loop var always has its last value

because they are evaluated only once, it does not affect loop controlThe loop var cannot be changed in the loop, but the parameters can;

Loop parameters are evaluated only once

• Counter-Controlled Loops

for Ada

var

(^) in

[

reverse

]

discrete_range

(^) loop

end loop

  1. Design choices: body (it is implicitly declared)Type of the loop var is that of the discrete range; its scope is the loop

The loop var does not exist outside the loop.

can; it does not affect loop control.The loop var cannot be changed in the loop, but the discrete range

The discrete range is evaluated just once

• Counter-Controlled Loops

C++ syntax: for

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

e.g.,sequences, with the statements separated by commasThe expressions can be whole statements, or even statement

for (i = 0, j = 10; j == i;

i++) ...

If the second expression is absent, it is an infinite loop.

  • for (cin >> x; x = -1; y += x) is legal, but doesn’t fitbased on a counter:might have nothing to do with incrementing and terminatingThere is no notion of “loop variable”, and expressions 2 and 3

the basic paradigm of counter-controlled loops

Logically-Controlled Loops

• Design issue: Pre-test (condition tested • While loops and their variants

tested at end)at beginning) or post-test (condition

Logically-Controlled Loops

Language Examples:

loop statements (Pascal has separate pretest and posttest logical

while

  • do (^) and

(^) repeat

  • until

like in the pretest case (expression for the posttest version is treated justC and C++ also have both, but the control

while

(^) do (^) and

(^) do (^) - (^) while

the beginning--Java has no goto)be Boolean (and the body can only be entered atJava is like C, except the control expression must

Ada has a pretest version, but no posttest

FORTRAN 77 and 90 have neither

User-Located Loop Control Mechanisms

  1. Ada - conditional or unconditional; for any loop;

any number of levels

for ... loop

LOOP1:

...

while ... loop

exit when ...

...

...

LOOP2:

end loop

for ... loop ...exit LOOP1 when .....

...end loop LOOP2;

end loop LOOP1;

User-Located Loop Control Mechanisms

2. (^) C , C++, and Java -

break

Unconditional; for any loop or

(^) switch

one level only (Java’s can have a label)

  • There is also has a

continue

(^) statement

iteration, but does not exit the loopfor loops; it skips the remainder of this

Unconditional Branching

The “Goto” statement

Problem:

(^) readability

JavaSome languages do not have them:e.g., Modula-2 and

  1. Unsigned int constants: Pascal (with colon) Label forms:

FORTRAN (no colon)

  1. Identifiers in2. Identifiers with colons: ALGOL 60, C

(^) << ... >>

: Ada

• Exception Handling

  • Language defined– User definedException:
  • User routines– Standard language routinesAn exception can be thrown by:

type of exceptionUser-written exception handlers can catch either

  • Graceful exit from segments of code which shouldn’t beAllows: executed in the face of an exception.
  • Programmer controlled handling of exceptions raised by standard library routines

int main(){ #include Exception Exception Handling: Library Raised try{int *p, n; while (true){ p = new int[n];cin >> n;cout << “Enter allocation request: “ << endl;

}

catch(bad_alloc) {cerr << “bad_alloc” << endl;}}

  • } “bad_alloc”Request for 1,000,000,000 integers results in a built-in exception:

with a segmentation fault, etc.This request is caught and the program terminates more gracefully than