block, also known as compound statement.
总体跟 C 保持一致,但是:
- nested scope can’t redefine an exsist name
”for each” Loop
switch case
A case
label can be
- A constant expression of type
char
,byte
,short
, orint
- An
enumerated
constant - Starting with Java 7, a
string
literal
labeled break
Scanner in = new Scanner(System.in);
int n;
read_data:
while (. . .) { // this loop statement is *tagged with the label*
. . .
for (. . .) { // this inner loop *is not labeled*
System.out.print("Enter a number >= 0: ");
n = in.nextInt();
if (n < 0) // should never happen—can't go on
break read_data; // break out of read_data loop . . .
}
} // this statement is executed immediately after the labeled break
if (n < 0) { // check for bad situation
// deal with bad situation
} else {
// carry out normal processing
}
Notice that the label must precede the outermost loop out of which you want to break. (估计就是在labeled break所在的loop前做标记)
也就是说这并不是一个 goto
,label仅仅只是对一块block做标记,当运行到labeled break的时候,就会走到 the end of the labeled block
goto
imitation
label:
{
/* to do something */
if (condition) break label; // exits block . . .
/* to do something */
}
// jumps here when the break statement executes
Note, however, that you can only jump out of a block, never into a block.