Java Reference
In-Depth Information
i in the condition , increment ,and loopBody ASTs properly reference the newly
declared loop index i. Since the loop termination condition is Boolean-valued
and non-constant, the loop is marked as terminating normally, and the loop
body is considered reachable. At the end of semantic checking, the scope
containing i is closed, guaranteeing that no subsequent references to the loop
index are possible.
A number of older languages, including Fortran, Pascal, Ada, Modula-2,
andModula-3, contain a more restrictive form of for loop. Typically, a variable
is identified as the ”loop index.” Initial and final index values are defined and
sometimes an increment value is specified. For example, in Pascal a for loop
is of the form:
for id := intialVal to finalVal do
loopBody
The loop index, id, must already be declared and must be a scalar type (in-
tegerorenumeration). TheinitialVal and finalVal expressions must be
semantically valid and have the same type as the loop index. Finally, the
loop index may not be changed within the loopBody. This can be enforced by
marking id's declaration as ”constant” or ”read only” while loopBody is being
analyzed.
9.1.5 Break, Continue, Return, and Goto Statements
Java contains no goto statement. It does, however, include breakand continue
statements, which are restricted forms of a goto,aswellasareturn statement.
We will consider the continue statement first.
Continue Statements
As with the continuestatement found inC andC
, Java's continue statement
attempts to ”continue with” the next iteration of a while, do,orfor loop. That
is, it transfers control to the bottom of a loop where the loop index is iterated
(in a for loop), and the termination condition is evaluated and tested.
A continue may only appear within a loop; this must be verified during
semantic analysis. Unlike C and C
++
, a loop label may be specified in a
continue statement. An unlabeled continue references the innermost for,
while, or do loop in which it is contained. A labeled continue references
the enclosing loop that has the corresponding label. Again, semantic analysis
must verify that an enclosing loop with the proper label exists.
Any statement in Java may be labeled. As shown in Figure 9.10 we will
assume an AST node LabeledStmtthat contains a string-valued field stmtLabel .
++
 
 
Search WWH ::




Custom Search