📄 tij0045.html
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>do-while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>do</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>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></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
sole difference between
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>do-while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is that the statement of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>do-while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
always executes at least once, even if the expression evaluates to false the
first time. In a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
if the conditional is false the first time the statement never executes. In
practice,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>do-while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is less common than
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc375545269"></a><a name="_Toc408018470"></a><P></DIV>
<A NAME="Heading129"></A><H3 ALIGN=LEFT>
for<P><A NAME="Index246"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loop performs initialization before the first iteration. Then it performs
conditional testing and, at the end of each iteration, some form of
“stepping.” The form 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 is:
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for(</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>initialization</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>Boolean-expression</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>step</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">Any
of the expressions
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>initialization</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Boolean-expression</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>step</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be empty. The expression is tested before each iteration, and as soon as it
evaluates to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>false</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
execution will continue at the line following the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement. At the end of each loop, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>step</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
executes.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loops are usually used for “counting” tasks:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: ListCharacters.java</font>
<font color="#009900">// Demonstrates "for" loop by listing</font>
<font color="#009900">// all the ASCII characters.</font>
<font color="#0000ff">public</font> <font color="#0000ff">class</font> ListCharacters {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
<font color="#0000ff">for</font>( <font color="#0000ff">char</font> c = 0; c < 128; c++)
<font color="#0000ff">if</font> (c != 26 ) <font color="#009900">// ANSI Clear screen</font>
System.out.println(
"value: " + (<font color="#0000ff">int</font>)c +
" character: " + c);
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that the variable
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is defined at the point where it is used, inside the control expression 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, rather than at the beginning of the block denoted by the open curly
brace. The scope of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
the expression controlled by the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</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">Traditional
procedural languages like C require that all <A NAME="Index247"></A>variables
be defined at the beginning of a block so when the compiler creates a block it
can allocate space for those variables. In Java and C++ you can spread your
variable declarations throughout the block, defining them at the point that you
need them. This allows a more natural coding style and makes code easier to
understand.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can define multiple variables within a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement, but they must be of the same type:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0, j = 1;
i < 10 && j != 11;
i++, j++)
<font color="#009900">/* body of for loop */</font>; </PRE></font><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"><B>int
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">definition
in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">statement
covers both
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>j</B></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">The
ability to define variables in the control expression is limited to 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. You cannot use this approach with any of the other selection or iteration
statements.
</FONT><a name="_Toc375545270"></a><P></DIV>
<A NAME="Heading130"></A><H4 ALIGN=LEFT>
The
comma operator
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Earlier
in this chapter I stated that the <A NAME="Index248"></A><A NAME="Index249"></A>comma
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>operator</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(not the comma
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>separator</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is used to separate function arguments) has only one use in Java: in the
control expression of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loop. In both the initialization and step portions of the control expression
you can have a number of statements separated by commas, and those statements
will be evaluated sequentially. The previous bit of code uses this ability.
Here’s another example:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: CommaOperator.java</font>
<font color="#0000ff">public</font> <font color="#0000ff">class</font> CommaOperator {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 1, j = i + 10; i < 5;
i++, j = i * 2) {
System.out.println("i= " + i + " j= " + j);
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here’s
the output:
</FONT><P></DIV>
<font color="#990000"><PRE>i= 1 j= 11
i= 2 j= 4
i= 3 j= 6
i= 4 j= 8 </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that in both the initialization and step portions the statements are
evaluated in sequential order. Also, the initialization portion can have any
number of definitions
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>of
one type
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc408018471"></a><P></DIV>
<A NAME="Heading131"></A><H3 ALIGN=LEFT>
break
and continue
<P><A NAME="Index250"></A><A NAME="Index251"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Inside
the body of any of the iteration statements you can also control the flow of
the loop by using
</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">.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
quits the loop without executing the rest of the statements in the loop.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
stops the execution of the current iteration and goes back to the beginning of
the loop to begin a new iteration.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
program shows examples of
</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">
within
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>for
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
</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">//: BreakAndContinue.java</font>
<font color="#009900">// Demonstrates break and continue keywords</font>
<font color="#0000ff">public</font> <font color="#0000ff">class</font> BreakAndContinue {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < 100; i++) {
<font color="#0000ff">if</font>(i == 74) <font color="#0000ff">break</font>; <font color="#009900">// Out of for loop</font>
<font color="#0000ff">if</font>(i % 9 != 0) <font color="#0000ff">continue</font>; <font color="#009900">// Next iteration</font>
System.out.println(i);
}
<font color="#0000ff">int</font> i = 0;
<font color="#009900">// An "infinite loop":</font>
<font color="#0000ff">while</font>(<font color="#0000ff">true</font>) {
i++;
<font color="#0000ff">int</font> j = i * 27;
<font color="#0000ff">if</font>(j == 1269) <font color="#0000ff">break</font>; <font color="#009900">// Out of loop</font>
<font color="#0000ff">if</font>(i % 10 != 0) <font color="#0000ff">continue</font>; <font color="#009900">// Top of loop</font>
System.out.println(i);
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
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 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">
never gets to 100 because the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement breaks out of the loop when
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is 74. Normally, you’d use a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -