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

📄 tij0045.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0044.html">Prev</a> | <a href="tij0046.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Execution
control
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
uses all of C&#8217;s execution control statements, so if you&#8217;ve
programmed with C or C++ then most of what you see will be familiar. Most
procedural programming languages have some kind of control statements, and
there is often overlap among languages. In Java, the keywords include 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>if-else</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>do-while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and a selection statement called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>switch</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Java does not, however, support the much-maligned 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(which can still be the most expedient way to solve certain types of problems).
You can still do a goto-like jump, but it is much more constrained than a
typical 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>goto</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc375545265"></a><a name="_Toc408018466"></a><P></DIV>
<A NAME="Heading124"></A><H3 ALIGN=LEFT>
true
and false
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
conditional statements use the truth or falsehood of a conditional expression
to determine the execution path. An example of a conditional expression is 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A
== B
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This uses the conditional operator 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>==</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to see if the value of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>A</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is equivalent to the value of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>B.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
The expression returns 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>true</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>false</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Any of the relational operators you&#8217;ve seen earlier in this chapter can
be used to produce a conditional statement. Note that Java doesn&#8217;t allow
you to use a number as a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
even though it&#8217;s allowed in C and C++ (where truth is nonzero and
falsehood is zero). If you want to use a non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
test, such as 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>if(a)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you must first convert it to a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
value using a conditional expression, such as 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>if(a
!= 0)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc375545266"></a><a name="_Toc408018467"></a><P></DIV>
<A NAME="Heading125"></A><H3 ALIGN=LEFT>
if-else<P><A NAME="Index242"></A><A NAME="Index243"></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>if-else</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement is probably the most basic way to control program flow. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>else</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is optional, so you can use 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>if</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in two forms:
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>if(</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Boolean-expression</I></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>statement</I></FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">or</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>if(</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Boolean-expression</I></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>statement</I></FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>else</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>statement</I></FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
conditional must produce a Boolean result. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>statement</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
means either a simple statement terminated by a semicolon or a compound
statement, which is a group of simple statements enclosed in braces. Anytime
the word &#8220;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>statement</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8221;
is used, it always implies that the statement can be simple or compound. 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
an example of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>if-else</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
here is a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
test(&#160;) 
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">method
that will tell you whether a guess is above, below, or equivalent to a target
number:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">static</font> <font color="#0000ff">int</font> test(<font color="#0000ff">int</font> testval) {
  <font color="#0000ff">int</font> result = 0;
  <font color="#0000ff">if</font>(testval &gt; target)
    result = -1;
  <font color="#0000ff">else</font> <font color="#0000ff">if</font>(testval &lt; target)
    result = +1;
  <font color="#0000ff">else</font>
    result = 0; <font color="#009900">// match</font>
  <font color="#0000ff">return</font> result;
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
is conventional to indent the body of a control flow statement so the reader
might easily determine where it begins and ends.
</FONT><P></DIV>
<A NAME="Heading126"></A><H4 ALIGN=LEFT>
return</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>return</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword has two purposes: it specifies what value a method will return (if it
doesn&#8217;t have a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>void</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
return value) and it causes that value to be returned immediately. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>test(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method above can be rewritten to take advantage of this:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">static</font> <font color="#0000ff">int</font> test2(<font color="#0000ff">int</font> testval) {
  <font color="#0000ff">if</font>(testval &gt; target)
    <font color="#0000ff">return</font> -1;
  <font color="#0000ff">if</font>(testval &lt; target)
    <font color="#0000ff">return</font> +1;
  <font color="#0000ff">return</font> 0; <font color="#009900">// match</font>
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
There&#8217;s no need for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>else</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
because the method will not continue after executing 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><a name="_Toc375545267"></a><a name="_Toc408018468"></a><P></DIV>
<A NAME="Heading127"></A><H3 ALIGN=LEFT>
Iteration
<A NAME="Index244"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>do-while</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">
control looping and are sometimes classified as 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>iteration
statements
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
A 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>statement</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
repeats until the controlling 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Boolean</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">-</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>expression</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
evaluates to false. The form for a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loop is
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while(</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Boolean-expression</I></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>statement</I></FONT><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>Boolean-expression</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is evaluated once at the beginning of the loop and again before each further
iteration of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>statement</I></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&#8217;s
a simple example that generates random numbers until a particular condition is
met:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: WhileTest.java</font>
<font color="#009900">// Demonstrates the while loop</font>

<font color="#0000ff">public</font> <font color="#0000ff">class</font> WhileTest {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    <font color="#0000ff">double</font> r = 0;
    <font color="#0000ff">while</font>(r &lt; 0.99d) {
      r = Math.random();
      System.out.println(r);
    }
  }
} <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>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>random(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Math</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
library, which generates a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>double
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">value
between 0 and 1. (It includes 0, but not 1.) The conditional expression for the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
says &#8220;keep doing this loop until the number is 0.99 or greater.&#8221;
Each time you run this program you&#8217;ll get a different-sized list of
numbers.
</FONT><a name="_Toc375545268"></a><a name="_Toc408018469"></a><P></DIV>
<A NAME="Heading128"></A><H3 ALIGN=LEFT>
do-while<A NAME="Index245"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
form for 

⌨️ 快捷键说明

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