Syntax
>>-continue-+-----------------+-;->< '-LabelIdentifier-'
Description
The continue keyword enables you to halt execution of a loop and resume
execution at the next loop iteration. A continue statement may occur only in
iteration statements; while, do, or for statements.
Control passes to the loop-continuation point of an iteration statement. A continue
statement with no label attempts to transfer control to the innermost enclosing while,
do, or for statement; this statement, which is called the continue
target, then immediately ends the current iteration and begins a new one. If no while,
do, or for statement encloses the continue
statement, a compilation error occurs.
A continue statement with label LabelIdentifier attempts to transfer control to the enclosing labeled loop statement that has the same LabelIdentifier; that statement, which is called the continue target, then immediately ends the continue iteration and begins a new one. The continue target must be a while, do, or for statement or a compilation error occurs. If no LabelIdentifier labeled statement contains the continue statement, a compilation error occurs.
The preceding descriptions say "attempts to transfer control" rather than just "transfers control" because if there are any try statements within the continue target whose try blocks contain the continue statement, then any finally clauses of those try statements are executed, in order, innermost to outermost, before control is transferred to the continue target. Abrupt completion of a finally clause can disrupt the transfer of control initiated by a continue statement.
Examples
The following example illustrates a basic use of the continue keyword:
/* * increments the variable a from 5 to 10, skips to 15, then * continues incrementing to 25 */ int a = 5; while (a <= 25) { System.out.println ("The value of a is " + a); if (a == 10) { a = 15; continue; } a++; }
Syntax diagrams
Labeled statements
do keyword
for keyword
try, catch, finally keywords
while keyword
Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.