⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0045.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
    <font color="#009900">// to labels here</font>
  }
  <font color="#0000ff">static</font> <font color="#0000ff">void</font> prt(String s) {
    System.out.println(s);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
uses the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>prt(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method that has been defined in the other examples.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that 
</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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loop, and that the increment-expression doesn&#8217;t occur until the end of
the pass through the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loop. Since 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
skips the increment expression, the increment is performed directly in the case
of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i
== 3
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue
outer
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement in the case of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>I
== 7 
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">also
goes to the top of the loop and also skips the increment, so it too is
incremented directly.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here
is the output:
</FONT><P></DIV>

<font color="#990000"><PRE>i = 0
<font color="#0000ff">continue</font> inner
i = 1
<font color="#0000ff">continue</font> inner
i = 2
<font color="#0000ff">continue</font>
i = 3
<font color="#0000ff">break</font>
i = 4
<font color="#0000ff">continue</font> inner
i = 5
<font color="#0000ff">continue</font> inner
i = 6
<font color="#0000ff">continue</font> inner
i = 7
<font color="#0000ff">continue</font> outer
i = 8
<font color="#0000ff">break</font> outer </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
not for the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break
outer
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement, there would be no way to get out of the outer loop from within an
inner loop, since 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
by itself can break out of only the innermost loop. (The same is true for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Of
course, in the cases where breaking out of a loop will also exit the method,
you can simply use a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>return</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here
is a demonstration of labeled 
</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">
statements with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loops:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: LabeledWhile.java</font>
<font color="#009900">// Java's "labeled while" loop</font>

<font color="#0000ff">public</font> <font color="#0000ff">class</font> LabeledWhile {
  <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="#0000ff">while</font>(<font color="#0000ff">true</font>) {
      prt("Outer <font color="#0000ff">while</font> loop");
      <font color="#0000ff">while</font>(<font color="#0000ff">true</font>) {
        i++;
        prt("i = " + i);
        <font color="#0000ff">if</font>(i == 1) {
          prt("<font color="#0000ff">continue</font>");
          <font color="#0000ff">continue</font>;
        }
        <font color="#0000ff">if</font>(i == 3) {
          prt("<font color="#0000ff">continue</font> outer");
          <font color="#0000ff">continue</font> outer;
        }
        <font color="#0000ff">if</font>(i == 5) {
          prt("<font color="#0000ff">break</font>");
          <font color="#0000ff">break</font>;
        }
        <font color="#0000ff">if</font>(i == 7) {
          prt("<font color="#0000ff">break</font> outer");
          <font color="#0000ff">break</font> outer;
        }
      }
    }
  }
  <font color="#0000ff">static</font> <font color="#0000ff">void</font> prt(String s) {
    System.out.println(s);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
same rules hold true for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	A
plain 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
goes to the top of the innermost loop and continues.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	A
labeled 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
goes to the label and re-enters the loop right after that label.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
&#8220;drops out of the bottom&#8221; of the loop.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	A
labeled 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
drops out of the bottom of the end of the loop denoted by the label. 
</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
output of this method makes it clear:
</FONT><P></DIV>

<font color="#990000"><PRE>Outer <font color="#0000ff">while</font> loop
i = 1
<font color="#0000ff">continue</font>
i = 2
i = 3
<font color="#0000ff">continue</font> outer
Outer <font color="#0000ff">while</font> loop
i = 4
i = 5
<font color="#0000ff">break</font>
Outer <font color="#0000ff">while</font> loop
i = 6
i = 7
<font color="#0000ff">break</font> outer </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It&#8217;s
important to remember that the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>only</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
reason to use labels in Java is when you have nested loops and you want to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
through more than one nested level.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
Dijkstra&#8217;s &#8220;goto considered harmful&#8221; paper, what he
specifically objected to was the labels, not the goto. He observed that the
number of bugs seems to increase with the number of labels in a program. Labels
and gotos make programs difficult to analyze statically, since it introduces
cycles in the program execution graph. Note that Java labels don&#8217;t suffer
from this problem, since they are constrained in their placement and
can&#8217;t be used to transfer control in an ad hoc manner. It&#8217;s also
interesting to note that this is a case where a language feature is made more
useful by restricting the power of the statement.
</FONT><a name="_Toc375545271"></a><a name="_Toc408018472"></a><P></DIV>
<A NAME="Heading133"></A><H3 ALIGN=LEFT>
switch<P><A NAME="Index256"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>switch</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is sometimes classified as a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>selection
statement
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>switch
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">statement
selects from among pieces of code based on the value of an integral expression.
Its form is:
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>switch(</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>integral-selector</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>)
{
</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>case</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>integral-value1</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
:
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>statement</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>;</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break;</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>case</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>
integral-value2
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>:</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>statement</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>;</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break;</B></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>case</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>integral-value3</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -