📄 tij0045.html
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
like this only if you didn’t know when the terminating condition was
going to occur. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement causes execution to go back to the top of the iteration loop (thus
incrementing
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
whenever
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is not evenly divisible by 9. When it is, the value is printed.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second portion shows an “infinite loop” that would, in theory,
continue forever. However, inside the loop there is a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement that will break out of the loop. In addition, you’ll see that
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
moves back to the top of the loop without completing the remainder. (Thus
printing happens only when the value of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is divisible by 9.) The output is:
</FONT><P></DIV>
<font color="#990000"><PRE>0
9
18
27
36
45
54
63
72
10
20
30
40</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
value 0 is printed because 0 % 9 produces 0.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
second form of the infinite loop is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for(;;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The compiler treats both
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while(true)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for(;;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the same way so whichever one you use is a matter of programming taste.
</FONT><P></DIV>
<A NAME="Heading132"></A><H4 ALIGN=LEFT>
The
infamous “goto”
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index252"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">keyword
has been present in programming languages from the beginning. Indeed,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
was the genesis of program control in assembly language: “if condition A,
then jump here, otherwise jump there.” If you read the assembly code that
is ultimately generated by virtually any compiler, you’ll see that
program control contains many jumps. However,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">jumps
at the source-code level, and that’s what brought it into disrepute. If a
program will always jump from one point to another, isn’t there some way
to reorganize the code so the flow of control is not so jumpy?
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">fell
into true disfavor with the publication of the famous “Goto considered
harmful” paper by Edsger Dijkstra, and since then goto-bashing has been a
popular sport, with advocates of the cast-out keyword scurrying for cover.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
is typical in situations like this, the middle ground is the most fruitful. The
problem is not the use of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
but the overuse of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and in rare situations
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is the best way to structure control flow.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Although
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
a reserved word in Java, it is not used in the language; Java has no
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
However, it does have something that looks a bit like a jump tied in with the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keywords. It’s not a jump but rather a way to break out of an iteration
statement.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
reason it’s often thrown in with discussions of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is because it uses the same mechanism: a label.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
<A NAME="Index253"></A>label
is an identifier followed by a colon, like this:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">label1:</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>only</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
place a label is useful in Java is right before an iteration statement. And
that means
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>right</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
before – it does no good to put any other statement between the label and
the iteration. And the sole reason to put a label before an iteration is if
you’re going to nest another iteration or a switch inside it.
That’s because the <A NAME="Index254"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index255"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keywords will normally interrupt only the current loop, but when used with a
label they’ll interrupt the loops up to where the label exists:
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>label1:
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>outer-iteration</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>{</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>inner-iteration</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>{</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
//...
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
break; // 1
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
//...
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
continue; // 2
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
//...
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
continue label1; // 3
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
//...
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
break label1; // 4
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
}
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>}</B></FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
case 1, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
breaks out of the inner iteration and you end up in the outer iteration. In
case 2, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
moves back to the beginning of the inner iteration. But in case 3, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue
label1
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
breaks out of the inner iteration
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>and</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the outer iteration, all the way back to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>label1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Then it does in fact continue the iteration, but starting at the outer
iteration. In case 4, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break
label1
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
also breaks all the way out to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>label1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but it does not re-enter the iteration. It actually does break out of both
iterations.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here
is an example using
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loops:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: LabeledFor.java</font>
<font color="#009900">// Java’s "labeled for loop"</font>
<font color="#0000ff">public</font> <font color="#0000ff">class</font> LabeledFor {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
<font color="#0000ff">int</font> i = 0;
outer: <font color="#009900">// Can't have statements here</font>
<font color="#0000ff">for</font>(; <font color="#0000ff">true</font> ;) { <font color="#009900">// infinite loop</font>
inner: <font color="#009900">// Can't have statements here</font>
<font color="#0000ff">for</font>(; i < 10; i++) {
prt("i = " + i);
<font color="#0000ff">if</font>(i == 2) {
prt("<font color="#0000ff">continue</font>");
<font color="#0000ff">continue</font>;
}
<font color="#0000ff">if</font>(i == 3) {
prt("<font color="#0000ff">break</font>");
i++; <font color="#009900">// Otherwise i never</font>
<font color="#009900">// gets incremented.</font>
<font color="#0000ff">break</font>;
}
<font color="#0000ff">if</font>(i == 7) {
prt("<font color="#0000ff">continue</font> outer");
i++; <font color="#009900">// Otherwise i never</font>
<font color="#009900">// gets incremented.</font>
<font color="#0000ff">continue</font> outer;
}
<font color="#0000ff">if</font>(i == 8) {
prt("<font color="#0000ff">break</font> outer");
<font color="#0000ff">break</font> outer;
}
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> k = 0; k < 5; k++) {
<font color="#0000ff">if</font>(k == 3) {
prt("<font color="#0000ff">continue</font> inner");
<font color="#0000ff">continue</font> inner;
}
}
}
}
<font color="#009900">// Can't break or continue</font>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -