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

📄 054-057.html

📁 dshfghfhhgsfgfghfhfghgfhfghfgh fg hfg hh ghghf hgf hghg gh fg hg hfg hfh f hg hgfh gkjh kjkh g yj f
💻 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=054-057//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="050-054.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="057-060.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>While and Do Statements</B></FONT></P>
<P>The syntax of the <I>while</I> statement is given by the following MBNF:</P>
<!-- CODE SNIP //-->
<PRE>
whileStatement -&gt;

&#147;while&#148;  &#147;(&#147; expression  &#147;)&#148; statement  .
</PRE>
<!-- END CODE SNIP //-->
<P>Like the <I>if</I> statement, the <I>while</I> statement must have a Boolean type expression. Also, the expression and ensuing statement will be evaluated as long as the Boolean expression returns true. If the expression returns false, the statement will not be executed. If the statement throws an exception, the <I>while</I> statement will also throw one. The <I>while</I> statement always evaluates the expression firs, before evaluating the statement. To change this order, use the <I>do</I> statement. The MBNF for the <I>do</I> statement is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
doStatement -&gt;

       &#147;do&#148; statement  &#147;while&#148;  &#147;(&#147; expression  &#147;)&#148;  &#147;;&#148;  .
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>do</I> statement will always evaluate the statement at least once before evaluating the <I>while</I> statement. The <I>do while</I> structure of Java is just like the <I>repeat until</I> structure of Pascal. It is a compile-time error to pass a non-Boolean type expression to the <I>do while</I>.</P>
<P>To make an infinite loop, just make the boolean expression a constant <I>true</I>:</P>
<!-- CODE SNIP //-->
<PRE>
while (true) &#123;
               draw();
               try &#123;Thread.sleep(1000);&#125;
               catch (InterruptedException e) &#123;&#125;
             &#125;
</PRE>
<!-- END CODE SNIP //-->
<P>It is common to embed assignments in the boolean expression given as an argument to the <I>while</I> statement. For example:</P>
<!-- CODE SNIP //-->
<PRE>
while ((next = tokens.nextToken()) != tokens.TT_EOF) &#123;
</PRE>
<!-- END CODE SNIP //-->
<P>Note the required use of the parentheses. The following would result in a compilation error:
</P>
<!-- CODE SNIP //-->
<PRE>
while (next = tokens.nextToken() != tokens.TT_EOF) &#123; //BUG!
</PRE>
<!-- END CODE SNIP //-->
<P>That&#146;s because the assignment operator takes lowest precedence.
</P>
<P>The <I>tokens.nextToken()</I> sets the <I>next</I> int to a value and checks the value against the <I>tokens.TT_EOF</I> class variable, an action that returns a boolean.</P>
<P><FONT SIZE="+1"><B>Switch</B></FONT></P>
<P>The <I>switch</I> statement transfers control to one of several statements depending on the value of an expression. It is a compile-time error if the expression is not constant or one of char, byte, short, or int. The following is the MBNF for the <I>switch</I> statement:</P>
<!-- CODE SNIP //-->
<PRE>
 switchStatement -&gt;
&#147;switch&#148;  &#147;(&#147; expression  &#147;)&#148; &#147;&#123;&#147;   &lt;  (  &#147;case&#148; expression &#147;:&#148;  )   |  (
&#147;default&#148;  &#147;:&#148; ) | statement &gt;   &#147;&#125;&#148;  .
</PRE>
<!-- END CODE SNIP //-->
<P>The use of the <I>switch</I> statement usually involves a <I>break</I>:</P>
<!-- CODE //-->
<PRE>
switch (expression) &#123;
     case const_1:
          statement1;
          break;
     case const_2:
          statement2;
          break;
     default:
          statement3;
          break;
&#125;
</PRE>
<!-- END CODE //-->
<P>If you omit the <I>break</I>, any remaining branches are executed. You can use only one default branch in the <I>switch</I> statement. It is typical in Java to have long <I>switch</I> statements when dispatching keyboard events. For example:</P>
<!-- CODE //-->
<PRE>
public boolean keyDown(Event e, int key) &#123;
     switch (key) &#123;
             case &#145;o&#146;:
                  openAudioStream();
                  return true;
             case &#145;v&#146;:
               saveAs();
                  return true;
             case &#145;p&#146;:
                  play();
                  return true;
             case &#145;n&#146;:
                  normalize();
                  return true;
             case &#145;m&#146;:
                  am();
                  return true;
             case &#145;f&#146;:
                  fm();
                  return true;
             case &#145;^&#146;:
                  sawWave();
                  return true;
             case &#145;e&#146;:
                  ulawData=Audio.encodeUlaw(doubleData);
                  return true;
             case &#145;1&#146;:
                  fft();
                  return true;
             case &#145;2&#146;:
                  ifft();
                  return true;
             case &#145;3&#146;:
                  dft();
                  return true;
             case &#145;t&#146;:
                  timeDelay();
                  return true;
             case &#145;g&#146;:
                  graphSound();
                  return true;
             case &#145;d&#146;:
                  doubleData = Audio.decodeUlaw(ulawData);
                  return true;
             case &#145;s&#146;:
                  sineWave();
                  return true;
             case &#145;[&#145;:
                  squareWave();
                  return true;
             case &#145;T&#146;:
                  triangleWave();
                  return true;
             case &#145;r&#146;:
                  ulawData = Audio.reverse(ulawData);
                  return true;
             case &#145;u&#146;:
                  graphUlaw();
                  return true;
        &#125;
</PRE>
<!-- END CODE //-->
<P>Keyboard shortcuts are popular with experienced users. It is unfortunate that the <I>switch</I> statement can take only scalar values. The same test is typically performed for menu-item instances, with lengthy code as the result.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="050-054.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="057-060.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>

<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright &copy; <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 + -