











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 19
This page cannot be seen from the preview
Don't miss anything!












th
st to Tuesday, April 9^
th
Should it be a discrete type?
Should it be restricted to integer?
Should it be local to the loop?
Upper bound parameter?
Upper bound parameter + 1?
Undefined?
good).Note: If scope restricted to loop, then this question goes away (which could be
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
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
yntax:
(^) DO (^) label var = start, finish [, stepsize]
Stepsize can be any value but zero
Parameters can be expressions
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
(^) in
reverse
(^) loop
end loop
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
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.
loop statements (Pascal has separate pretest and posttest logical
while
(^) repeat
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
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 -
(^) switch
(^) statement
iteration, but does not exit the loopfor loops; it skips the remainder of this
The “Goto” statement
Problem:
(^) readability
JavaSome languages do not have them:e.g., Modula-2 and
FORTRAN (no colon)
(^) << ... >>
: Ada
int main(){ #include
}
catch(bad_alloc) {cerr << “bad_alloc” << endl;}}
with a segmentation fault, etc.This request is caught and the program terminates more gracefully than