Java Reference
In-Depth Information
by the loop control condition and the loop body. Throws analysis again
uses
gather
T
hrows
to gather and return each subtree's throwsSet (Figure 9.4,
Marker 11 ).
As an example, consider the following statement:
while (i >= 0) {
a[i--] = 0; }
The control expression, i>=0, is first checked to see if it is a valid Boolean-
valued expression. Then, the loop body is checked for possible semantic errors.
Since the control expression is non-constant, the loop body is assumed to be
reachable and the loop is marked as terminating normally.
Do-While and Repeat Loops
Java,C,andC
contain a variant of the while loop, the do-while loop. A
do-while loop is simply a while loop that evaluates and tests its termination
condition after executing the loop body rather than before. Assuming the
same AST structure as a while loop, the semantic analysis visitor (Figure 9.1,
Marker 4 ) and throws analysis visitor (Figure 9.4, Marker 12 )areidentical
to those of the while loop.
The reachability visitor for a do-while is shown in Figure 9.7. Reachability
rules for a do-while loop di
++
er from those of a while loop. Since the loop body
always executes at least once, the special case of a false control expression can
be ignored. Initially, terminatesNormally is set to false (Marker 25 ). It can
be reset to true during reachability analysis of the loop body (Marker 26 )if
the body contains a reachable break statement. For non-constant loop con-
trol expressions, a do-while loop terminates normally if the loop body does
(Marker 27 ).
A number of languages, including Pascal and Modula-3, contain a repeat-
until loop. This is essentially a do while loop except for the fact that the loop
is terminated when the control condition becomes true rather than false. The
semantic analysis of a repeat-until loop is almost identical to that of a do while
loop. The only change is that the special case of a non-terminating loop occurs
when the control expression is false rather than true.
ff
9.1.4 For Loops
For loops are normally used to step an index variable through a range of
values. However, for loops in C, C
, and Java are really a generalization
of while loops. Consider the AST for a for loop, as shown in Figure 9.9.
++
,C
 
 
Search WWH ::




Custom Search