📄 057-060.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Java Programming: The Basics</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=2//-->
<!--PAGES=057-060//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="054-057.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="060-064.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>For</B></FONT></P>
<P>The <I>for</I> statement is 10 times more common than the <I>while</I> statement in the DiffCAD program. The MBNF for the <I>for</I> statement follows:</P>
<!-- CODE //-->
<PRE>
forStatement ->
“for” “(“ ( variableDeclaration | ( expression “;” )
| “;” ) [expression] “;” [expression] “;” “)”
statement .
variableDeclaration ->
< modifier > type variableDeclarator < “,”
variableDeclarator > “;” .
variableDeclarator ->
identifier < “[“ “]” > [ “=” variableInitializer] .
variableInitializer ->
expression | ( “{“ [variableInitializer < “,”
variableInitializer > [ “,” ] ] “}” ) .
</PRE>
<!-- END CODE //-->
<P>If the test is not a Boolean type expression, a compile-time error results. If the test is not satisfied, the statement will not execute. The statement need not execute, at all. Here are examples of the <I>for</I> statement:</P>
<!-- CODE SNIP //-->
<PRE>
for (i=1; i < 99; i++) {
}; // null statement
for (;;) { // an infinite loop
if (expression) { break}
// more junk here
}
for (int i = 99; i < 50; i++)
System.out.println(“I never printed”);
</PRE>
<!-- END CODE SNIP //-->
<P>The comma (,) is permitted in the initialization and increment section of the <I>for</I> loop. For example:</P>
<!-- CODE SNIP //-->
<PRE>
for (i=0, j=10; i < 100; i++, j +=2) {
//more stuff here
}
</PRE>
<!-- END CODE SNIP //-->
<P>If the initializations or update parts of the <I>for</I> loop throw an exception, the <I>for</I> loop will throw an exception. The <I>for</I> loop is the only statement in Java that uses the comma as a separator.</P>
<P><FONT SIZE="+1"><B>Continue</B></FONT></P>
<P>The <I>continue</I> statement in Java aborts an iteration. It is a compile-time error to have a <I>continue</I> in something other than a <I>while</I>, <I>do</I>, or <I>for</I> statement. A <I>continue</I> statement with no label identifier proceeds to the next enclosing iteration. A <I>continue</I> statement with a label identifier proceeds to the next enclosing <I>labeled</I> statement. The MBNF for the continue statement follows:</P>
<!-- CODE SNIP //-->
<PRE>
continue_statement ->
“continue” [identifier] “;”
</PRE>
<!-- END CODE SNIP //-->
<P>For example:
</P>
<!-- CODE //-->
<PRE>
foo: for (int i = 1; i < 5; i ++) {
for (int j=1; j < 5; j++) {
if ( i % j == 2) {
System.out.println(“continue”);
continue foo;
}
System.out.print(i*j + “ “);
}
System.out.println();
}
</PRE>
<!-- END CODE //-->
<P>The preceding code outputs the following:
</P>
<!-- CODE SNIP //-->
<PRE>
1 2 3 4
2 4 continue
3 6 9 12
4 8 12 16
</PRE>
<!-- END CODE SNIP //-->
<P>Here’s more typical usage:
</P>
<!-- CODE SNIP //-->
<PRE>
for (int i = 1; i < constantPool.length; i++) {
if (constantPool[i] == null)
continue;
// more stuff follows
}
</PRE>
<!-- END CODE SNIP //-->
<P>Here, <I>continue</I> will proceed to the next <I>i</I> without finishing the rest of the loop.</P>
<P>The labeled <I>continue</I> appears to be less popular than the unlabeled <I>continue</I>. DiffCAD does not have a single use of the labeled <I>continue</I>.</P>
<P><FONT SIZE="+1"><B>Break</B></FONT></P>
<P>The <I>break</I> statement appears much more often in DiffCAD than the <I>continue</I> statement (345 times vs. 15 times). Here’s the MBNF for the <I>break</I> statement:</P>
<!-- CODE SNIP //-->
<PRE>
break_statement ->
“break” [identifier] “;”
</PRE>
<!-- END CODE SNIP //-->
<P>It is a compile-time error not to enclose <I>break</I> within a break target. Valid break targets are labels, <I>switch</I>, <I>while</I>, <I>do</I>, or <I>for</I> statements. <I>Break</I> can be used with or without an identifier label. <I>Break</I> causes control to pass to the innermost enclosing break target. The break target completes normally. Compare this to a <I>continue</I> statement in a loop. <I>Continue</I> will continue with the loop; <I>break</I> will break out of it.</P>
<P>The following example permits the <I>for</I> loop to terminate normally for two reasons. The first is the <I>for</I> loop expression, <I>n < 2*Math.PI</I>, which is tested only at the top of the loop. The second is the <I>(i >= out.length)</I> expression, which is tested at the bottom of the loop.</P>
<!-- CODE SNIP //-->
<PRE>
done: for (double n = 0; n < 2*Math.PI; n = n + step)
{
out[i] = in[i]* Math.sin(n);
i++;
if (i >= out.length) { break done;}
}
</PRE>
<!-- END CODE SNIP //-->
<P>In the following case, the <I>break</I> is removed by adding a more complex test in the <I>for</I> statement.</P>
<!-- CODE SNIP //-->
<PRE>
for (double n = 0;
(n < 2*Math.PI) && (i < out.length);
n += step, i++) {
out[i] = in[i]* Math.sin(n);
}
</PRE>
<!-- END CODE SNIP //-->
<P>Sometimes the <I>break</I> statement is essential, as in a <I>switch</I> statement.</P>
<P><FONT SIZE="+1"><B>Return</B></FONT></P>
<P>The <I>return</I> statement in Java takes an optional expression. It transfers control to the invoker. The MBNF for the return statement follows:</P>
<!-- CODE SNIP //-->
<PRE>
return_statement ->
“return” [expression] “;”
</PRE>
<!-- END CODE SNIP //-->
<P>When the expression is omitted, <I>void</I> is returned.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="054-057.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="060-064.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -