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

📄 tut2-1.html

📁 a Complete C++ language tutorial on the cplusplus.com
💻 HTML
📖 第 1 页 / 共 2 页
字号:
instruction in any of the fields included in a <TT><B>for</B></TT> loop, like in
<TT><I>initialization</I></TT>, for example. The comma operator (<TT><B>,</B></TT>) is an
instruction separator, it serves to separate more than one instruction where only
one instruction is generally expected. For example, suppose that we wanted to intialize
more than one variable in our loop:
<BLOCKQUOTE><TT><PRE>
for ( n=0, i=100 ; n!=i ; n++, i-- )
{
  <FONT COLOR="green"><I>// whatever here...</I></FONT>
}
</PRE></TT></BLOCKQUOTE>
This loop will execute 50 times if neither <TT><B>n</B></TT> nor <TT><B>i</B></TT> are
modified within the loop:
<BLOCKQUOTE><IMG SRC="imgloop1.gif"></BLOCKQUOTE>
<TT><B>n</B></TT> starts with <TT><B>0</B></TT> and <TT><B>i</B></TT> with
<TT><B>100</B></TT>, the condition is <TT><B>(n!=i)</B></TT>
(that <TT><B>n</B></TT> be not equal to <TT><B>i</B></TT>). Beacuse <TT><B>n</B></TT>
is increased by one and <TT><B>i</B></TT> decreased by one, the loop's condition will become
<TT>false</TT> after the 50th loop, when both <TT><B>n</B></TT> and <TT><B>i</B></TT>
will be equal to 50.
</dl>

<p>
<h2>Bifurcation of control and jumps.</h2>
<dl>
<dt><b>The <i>break</i> instruction.</b><br>
<dd>
Using <i>break</i> we can leave a loop even if the condition for its end is not fulfilled.
It can be used to end an infinite loop, or to force it to end before its natural end.
For example, we are going to stop the count down before it naturally finishes
(an engine failure maybe):
<P><CENTER>
<TABLE WIDTH=90% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// break loop example</I>
#include &lt;iostream.h&gt;
int main ()
{
  int n;
  for (n=10; n&gt;0; n--) {
    cout &lt;&lt; n &lt;&lt; ", ";
    if (n==3)
    {
      cout &lt;&lt; "countdown aborted!";
      break;
    }
  }
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<B><TT>10, 9, 8, 7, 6, 5, 4, 3, countdown aborted! </TT></B>
</TD></TR></TABLE>
</CENTER>
<P>

<dt><b>The <i>continue</i> instruction.</b><br>
<dd>
The <i>continue</i> instruction causes the program to skip the rest of the loop
in the present iteration as if the end of the <TT><I>statement</I></TT> block would
have been reached, causing it to jump to the following iteration. For example, we are going
to skip the number 5 in our countdown:
<P><CENTER>
<TABLE WIDTH=90% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// break loop example</I>
#include &lt;iostream.h&gt;
int main ()
{
  for (int n=10; n&gt;0; n--) {
    if (n==5) continue;
    cout &lt;&lt; n &lt;&lt; ", ";
  }
  cout &lt;&lt; "FIRE!";
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<B><TT>10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!</TT></B>
</TD></TR></TABLE>
</CENTER>

<p>
<dt><b>The <i>goto</i> instruction.</b><br>
<dd>
It allows making an absolute jump to another point in the program. You should use this
feature carefully since its execution ignores any type of nesting limitation.
<P>
The destination point is identified by a label, which is then used as an argument
for the goto instruction. A label is made of a valid identifier followed by a
colon (<TT><B>:</B></TT>).

<p>
This instruction does not have a concrete utility in structured or object oriented
programming aside from those that low-level programming fans may find for it.
For example, here is our countdown loop using
<tt><b>goto</b></tt>:

<P><CENTER>
<TABLE WIDTH=90% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// goto loop example</I>
#include &lt;iostream.h&gt;
int main ()
{
  int n=10;
  loop:
  cout &lt;&lt; n &lt;&lt; ", ";
  n--;
  if (n>0) goto loop;
  cout &lt;&lt; "FIRE!";
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<B><TT>10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!</TT></B>
</TD></TR></TABLE>
</CENTER>

<p>
<dt><b>The <i>exit</i> function.</b><br>
<dd>
<i>exit</i> is a function defined in 
<A HREF="/ref/cstdlib"><B><TT>cstdlib</TT></B></A> (stdlib.h) library.
<p> The purpose of <i>exit</i> is to terminate the running program with an
specific exit code. Its prototype is:<br>
<blockquote><tt>
<b>void exit (int </b><i>exit code</i><b>);</b>
</tt></blockquote>
The <TT><I>exit code</I></TT> is used by some operating systems and may be used by calling
programs. By convention, an <TT><I>exit code</I></TT> of 0 means that the
program finished normally and any other value means an error happened.
</dl>

<p>
<h2>The selective Structure: <i>switch</i>.</h2>
The syntax of the <I>switch</I> instruction is a bit peculiar.
Its objective is to check several possible constant values for an expression, something
similar to what we did at the beginning of this section with the linking of several
<i>if</i> and <i>else if</i> sentences.  Its form is the following:<br>
<blockquote>
<tt><b>switch (</b><i>expression</i><b>) {<br>
&nbsp; case </b><i>constant1</i><b>:<br>
</b><i>&nbsp; &nbsp; block of instructions 1</i><b><br>
&nbsp; &nbsp; break;<br>
&nbsp; case </b><i>constant2</i><b>:<br>
</b><i>&nbsp; &nbsp; block of instructions 2</i><b><br>
&nbsp; &nbsp; break;<br>
&nbsp; .<br>
&nbsp; .<br>
&nbsp; .<br>
&nbsp; default:<br>
</b><i>&nbsp; &nbsp; default block of instructions</i><b><br>
&nbsp; }</b></tt><br>
</blockquote>
It works in the following way: <b>switch</b> evaluates <tt><i>expression</i></tt> and
checks if it is equivalent to
<tt><i>constant1</i></tt>, if it is, it executes <tt><i>block of instructions 1</i></tt>
until it finds the <b>break</b> keyword, then the program will
jump to the end of the <I>switch</I> selective structure.<br>
If <TT><I>expression</I></TT> was not equal to <TT><I>constant1</I></TT>
it will check if <tt><i>expression</i></tt> is equivalent to <tt><i>constant2</i></tt>.
If it is, it will execute <tt><i>block of instructions 2</i></tt> until it
finds the <b>break</b> keyword.<br>
Finally, if the value of <tt><i>expression</i></tt> has not matched any of the
previously specified constants (you may specify as many <B>case</B> sentences as
values you want to check), the program will execute the instructions included in the
<b>default:</b> section, if this one exists, since it is optional.

</blockquote>
<p>
Both of the following code fragments are equivalent:
<P><CENTER>
<TABLE WIDTH=90% CELLSPACING=5 CELLPADDING=5>
<TR><TD ALIGN="center" WIDTH=50% ><B><U><I>switch</I> example</U></B></TD>
<TD ALIGN="center" WIDTH=50%><B><U><I>if-else</I> equivalent</U></B></TD></TR>
<TR><TD VALIGN="top" WIDTH=50% BGCOLOR="#FFFFBF">
<TT><PRE>switch (x) {
  case 1:
    cout &lt;&lt; "x is 1";
    break;
  case 2:
    cout &lt;&lt; "x is 2";
    break;
  default:
    cout &lt;&lt; "value of x unknown";
  }
</PRE></TT>
</TD>
<TD VALIGN="top" WIDTH=50% BGCOLOR="#FFFFBF">
<TT><PRE>if (x == 1) {
  cout &lt;&lt; "x is 1";
  }
else if (x == 2) {
  cout &lt;&lt; "x is 2";
  }
else {
  cout &lt;&lt; "value of x unknown";
  }
</PRE></TT>
</TD></TR></TABLE></CENTER>

<p>
I have commented before that the syntax of the <b><I>switch</I></b> instruction is a
bit peculiar. Notice the inclusion of the <b>break</b> instructions at the end of each
block.  This is necessary because if, for example, we did not include it after
<tt><i>block of instructions 1</i></tt> the program would not jump to the end of the
switch selective block (<b><tt>}</tt></b>) and it would continue executing the rest of
the blocks of instructions until the first appearance of the <b>break</b> instruction or
the end of the switch selective block.  This makes it unnecessary to include
curly brackets <b><tt>{ }</tt></b> in each of the cases, and it can also be useful to
execute the same block of instructions for different possible values for the expression
evaluated. For example:

<BLOCKQUOTE><TABLE CELLPADDING=5><TR><TD BGCOLOR="#FFFFBF">
<TT><PRE>switch (x) {
  case 1:
  case 2:
  case 3:
    cout &lt;&lt; "x is 1, 2 or 3";
    break;
  default:
    cout &lt;&lt; "x is not 1, 2 nor 3";
  }
</PRE></TT>
</TD></TR></TABLE></BLOCKQUOTE>

Notice that <TT><B>switch</B></TT> can only be used to compare an
expression with different <U>constants</U>.
Therefore we cannot put variables (<b><tt>case (n*2):</tt></b>) or
ranges (<B><TT>case (1..3):</TT></B>) because they are not valid constants.
<p>
If you need to check ranges or values that are not constants use a concatenation
of <tt><b>if</b></tt> and <tt><b>else if</b></tt> sentences.

<!--cuatut-->
<P>
<CENTER><TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
 <TR><TD BGCOLOR="#0000FF"><IMG SRC="head0.gif" WIDTH=2 HEIGHT=2></TD></TR>
 <TR><TD ALIGN="right"><FONT FACE="arial,helvetica" SIZE=1>&copy; The C++ Resources Network, 2000-2003 - All rights reserved</FONT></TD></TR>
</TABLE></CENTER>
<P>
<CENTER>
<TABLE CELLPADDING=0 WIDTH=100%>
<TR><TD ALIGN="right" WIDTH=45%><A HREF="tut1-4.html">
 <IMG SRC="butnback.gif" ALIGN="right" BORDER=0>
 Previous:<BR><B>1-4. Comunication throgh console.</B></A></TD>
<TD ALIGN="center" WIDTH=10%><A HREF="index.html">
 <IMG SRC="butnindx.gif" BORDER=0><BR>
 index</A></TD>
<TD ALIGN="left" WIDTH=45%><A HREF="tut2-2.html">
 <IMG SRC="butnnext.gif" ALIGN="left" BORDER=0>
 Next:<BR><B>2-2. Functions.</B></A>
</TD></TR></TABLE>
</CENTER>
<!--/cuatut-->

</body>
</html>

⌨️ 快捷键说明

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