📄 050-054.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=050-054//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="047-050.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="054-057.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Flow of Control</FONT></H4>
<P>This section describes Java’s features for altering the flow of control. Java is strongly typed, so <I>if</I>, <I>for</I>, and <I>while</I> statements require Boolean expressions as arguments. In fact, all flow of control statements require Boolean expressions, with the single exception of the <I>switch</I> statement, which permits an integer type expression.</P>
<P><FONT SIZE="+1"><B>Expressions</B></FONT></P>
<P>Central to the use of the flow of control is the notion of a Boolean type expression. Unlike C and C++, Java will not accept an expression that is of integer type. The following MBNF defines the expression statement in Java along with the testing expression and the logical expression:
</P>
<!-- CODE //-->
<PRE>
expression ->
numericExpression
| testingExpression | logicalExpression |
stringExpression | bitExpression |
castingExpression | creatingExpression |
literalExpression | "null" |
"super" | "this" |
identifier | ( "(" expression ")" ) |
( expression ( ( "(" [arglist] ")" ) |
( "[" expression "]" ) | ( "." expression ) |
( "," expression ) |
( "instanceof" ( className | interfaceName ) ) ) ) .
numericExpression ->
( ( "-" | "++" | "--" ) expression ) |
( expression ( "++" | "--" ) ) |
( expression ( "+" | "+=" | "-"
| "-=" | "*" | "*=" | "/" | "/=" | "%" | "%=" )
expression ) .
testingExpression ->
( expression
( ">" | "<" | ">=" | "<=" | "==" | "!=" )
expression ) .
logicalExpression ->
( "!" expression ) | ( expression
( "&" | "&=" | "|" | "|=" | "^" | "^=" |
( "&&" ) | "||=" | "%" | "%=" ) expression ) |
( expression "?" expression ":" expression ) | "true" | "false" .
</PRE>
<!-- END CODE //-->
<P>In the following sections we give examples of expressions that alter the flow of control.
</P>
<P><FONT SIZE="+1"><B>If</B></FONT></P>
<P>The <I>if</I> statement in Java requires a boolean type expression:</P>
<!-- CODE SNIP //-->
<PRE>
ifStatement ->
“if” “(“ expression
“)” statement [ “else”
statement] .
if (type.compareTo(“ConstantValue”) == 0) {
if (i >= out.length) {
if (e.target == saveSound_mi) {
if (e.target == graphSound_mi) {
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>then</I> part of the <I>if</I> may take the shape of any valid statement. No {} characters are required:</P>
<!-- CODE SNIP //-->
<PRE>
if (Math.abs(return_val) < 0.99)
return return_val;
else return 0;
</PRE>
<!-- END CODE SNIP //-->
<P>An extended clause is often indented to indicate when the clause has ended. Sometimes it is good style to indicate the end of the clause with a comment. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
if (interfaces == null) dos.writeShort(0);
else {
dos.writeShort(interfaces.length);
for (int i = 0; i < interfaces.length; i++)
dos.writeShort(
ConstantPoolInfo.indexOf(
interfaces[i], constantPool));
} // end else
</PRE>
<!-- END CODE SNIP //-->
<P>This practice is particularly good when the beginning of a clause is off the screen. Poor use of indentation and comments can make code hard to read. Consider the following example:
</P>
<!-- CODE SNIP //-->
<PRE>
if(x1 < datarect.x) x1 = datarect.x;
else if(x1 > datarect.x + datarect.width )
x1 = datarect.x + datarect.width;
if(y1 < datarect.y) y1 = datarect.y;
else if(y1 > datarect.y + datarect.height )
y1 = datarect.y + datarect.height;
</PRE>
<!-- END CODE SNIP //-->
<P>Now compare the preceding code with the following code:
</P>
<!-- CODE SNIP //-->
<PRE>
if (x1 < datarect.x) x1 = datarect.x;
else if (x1 > datarect.x + datarect.width )
x1 = datarect.x + datarect.width;
if (y1 < datarect.y) y1 = datarect.y;
else if (y1 > datarect.y + datarect.height )
y1 = datarect.y + datarect.height;
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B><I>Else if</I> is used as a word pair, significantly cleaning up and shortening the code.<HR></FONT>
</BLOCKQUOTE>
<P>This example, is a long <I>if else</I> chain:</P>
<!-- CODE //-->
<PRE>
if(xminText.equals(e.target)) {
xmaxText.requestFocus();
return true;
} else
if(xmaxText.equals(e.target)) {
yminText.requestFocus();
return true;
} else
if(yminText.equals(e.target)) {
ymaxText.requestFocus();
return true;
} else
if(ymaxText.equals(e.target)) {
xminText.requestFocus();
return true;
}
</PRE>
<!-- END CODE //-->
<P>Now let’s convert it into the <I>else if</I> style:</P>
<!-- CODE //-->
<PRE>
if(xminText.equals(e.target)) {
xmaxText.requestFocus();
return true;
} else if(xmaxText.equals(e.target)) {
yminText.requestFocus();
return true;
} else if(yminText.equals(e.target)) {
ymaxText.requestFocus();
return true;
} else if(ymaxText.equals(e.target)) {
xminText.requestFocus();
return true;
}
</PRE>
<!-- END CODE //-->
<P>Such long dispatches are common in Java.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="047-050.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="054-057.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 + -