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

📄 tut2-1.html

📁 a Complete C++ language tutorial on the cplusplus.com
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html>
<head>
<title>C++ Tutorial: 2.1, Control Structures.</title>
<META NAME="description" CONTENT="Conditional, repetitive and selective structures: if-else, while, do-while, for, switch. Loops, bifurcation and jumps: break, continue, goto, exit">
<META NAME="keywords" CONTENT="conditions loops">
</head>

<body bgcolor="white">

<!--captut-->
<CENTER>
<TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=1 BORDER=0>
<TR><TD WIDTH=90%>
 <FONT SIZE=4> Section 2.1 </FONT><BR>
 <FONT SIZE=5><B> Control Structures. </B></FONT>
</TD><TD><!--ad--><!--#include virtual="/ad/ad468.shtml"--><!--/ad-->
</TD><TD VALIGN="bottom"><A HREF="http://www.cplusplus.com/doc/tutorial/">
 <IMG SRC="head.gif" ALT="cplusplus.com" BORDER=0></A></TD></TR>
<TR><TD BGCOLOR="#0000FF" ALIGN="center" COLSPAN=3>
 <IMG SRC="head0.gif" WIDTH=2 HEIGHT=2 BORDER=0></TD></TR>
</TABLE>
</CENTER>
<!--/captut-->

<p>
A program is usually not limited to a linear sequence of instructions. During its
process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides
control structures that serve to specify what has to be done to perform our program.

<p>
With the introduction of control sequences we are going to have to introduce
a new concept:
the <b><i>block of instructions</i></b>. A block of instructions is a group of instructions
separated by semicolons (<tt>;</tt>) <U>but</U> grouped in a block delimited by
curly bracket signs: <b><tt>{</tt></b> and <b><tt>}</tt></b>.

<p>
Most of the control structures that we will see in this section allow a generic
<tt><b><i>statement</i></b></tt> as a parameter, this refers to either a single instruction
or a <U>block of instructions</U>, as we want. If we want the statement to be
a single instruction we do not need to enclose it between curly-brackets
(<TT><B>{}</B></TT>). If we want the
statement to be more than a single instruction we must enclose them
between curly brackets (<TT><B>{}</B></TT>) forming a block of instructions.

<p>
<h2>Conditional structure: <i>if</i> and <i>else</i></h2>
It is used to execute an instruction or block of instructions only if a
condition is fulfilled.  Its form is:<br>
<blockquote><tt><b>if (</b><i>condition</i><b>) </b><i>statement</i></tt><br></blockquote>
where <tt><i>condition</i></tt> is the expression that is being evaluated.
If this condition is <TT><B>true</B></TT>, <TT><B><I>statement</I></B></TT> is executed.
If it is false, <tt><i>statement</i></tt> is ignored (not executed) and the program
continues on the next instruction after the conditional structure.
<p>
For example, the following code fragment prints out <tt><b>x is 100</b></tt> only
if the value stored in variable <TT><B>x</B></TT> is indeed 100:<br>
<blockquote>
<tt>if (x == 100)<br>&nbsp; cout &lt;&lt; "x is 100";</tt><br>
</blockquote>
If we want more than a single instruction to be executed in case that
<tt><i>condition</i></tt> is <TT><B>true</B></TT> we can specify a
<i>block of instructions</i> using curly brackets <tt>{ }</tt>:
<blockquote>
<tt>if (x == 100)<br>&nbsp;{<br>&nbsp; cout &lt;&lt; "x is ";<br>
&nbsp; cout &lt;&lt; x;<br>&nbsp;}</tt><br>
</blockquote>

<p>
We can additionally specify what we want that happens if the condition
is not fulfilled by using the keyword <i>else</i>. Its form used in conjunction
with <TT><B>if</B></TT> is:<br>
<blockquote>
<tt><b>if (</b><i>condition</i><b>) </b><i>statement1</i><b> else </b><i>statement2</i></tt><br>
</blockquote>
For example:<br>
<blockquote>
<tt>if (x == 100)<br>&nbsp; cout &lt;&lt; "x is 100";<br>else<br>&nbsp; cout &lt;&lt; "x is not 100";</tt><br>
</blockquote>
prints out on the screen <tt><b>x is 100</b></tt> if indeed x is worth 100, but if it is
not -and only if not- it prints out <tt><b>x is not 100</b></tt>.

<p>
The <i>if</i> + <i>else</i> structures can be concatenated with the intention of verifying
a range of values. The following example shows its use telling if the present value
stored in <b><tt>x</tt></b> is positive, negative or none of the previous, that is to say,
equal to zero.<br>
<blockquote>
<tt>if (x &gt; 0)<br>&nbsp; cout &lt;&lt; "x is positive";<br>
else if (x < 0)<br>&nbsp; cout &lt;&lt; "x is negative";<br>
else<br>&nbsp; cout &lt;&lt; "x is 0";</tt><br>
</blockquote>
Remember that in case we want more than a single instruction to be executed, we must
group them in a <i>block of instructions</i> by using curly brackets <tt><B>{ }</B></tt>.

<p>
<h2>Repetitive structures or loops</h2><br>
<i>Loops</i> have as objective to repeat a <I>statement</I> a certain number of times
or while a condition is fulfilled.

<dl>
<p>
<dt><b>The <i>while</i> loop.</b><br>
<dd>
Its format is:<br>
<blockquote>
<tt><b> while (</b><i>expression</i><b>) </b><i>statement</i></tt><br>
</blockquote>
and its function is simply to repeat <tt><i>statement</i></tt> while
<tt><i>expression</i></tt> is true.
<p>
For example, we are going to make a program to count down using a <I>while</I> loop:<br>
<P><CENTER>
<TABLE WIDTH=90% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// custom countdown using while</I>
#include &lt;iostream.h&gt;
int main ()
{
  int n;
  cout &lt;&lt; "Enter the starting number &gt; ";
  cin &gt;&gt; n;
  while (n&gt;0) {
    cout &lt;&lt; n &lt;&lt; ", ";
    --n;
  }
  cout &lt;&lt; "FIRE!";
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top"><TT>
<B>Enter the starting number &gt; </B>8<BR>
<B><TT>8, 7, 6, 5, 4, 3, 2, 1, FIRE!</TT></B>
</TD></TR></TABLE>
</CENTER>
<P>
When the program starts the user is prompted to insert a starting number for the countdown.
Then the <tt><i>while</i></tt> loop begins, if the value entered by the user
fulfills the condition <TT><B>n&gt;0</B></TT> (that <tt><b>n</b></tt> be greater than
<tt><b>0</b></tt> ),
the block of instructions that follows will execute an indefinite number of times while the condition
<tt><b>(n&gt;0)</b></tt> remains true.
<p>
 All the process in the program above can be interpreted according to the following script:
beginning in <tt><b>main</b></tt>:
<ul>
<li><b>1.</b> User assigns a value to <tt><b>n</b></tt>.
<li><b>2.</b> The while instruction checks if <tt><b>(n&gt;0)</b></tt>. At this point
there are two possibilities: 
<ul><li><b>true:</b> execute <i>statement</i> (step <b>3,</b>)
<li><b>false:</b> jump <i>statement</i>.  The program follows in step <tt><b>5.</b></tt>.</ul>
<li><b>3.</b> Execute <i>statement</i>:<br>
 <tt> cout &lt;&lt; n &lt;&lt; ", ";<br> --n;</tt><br>
 (prints out <tt><b>n</b></tt> on screen and decreases <TT><B>n</B></TT> by 1).
<li><b>4.</b> End of block. Return Automatically to step <b>2.</b>
<li><b>5.</b> Continue the program after the block: print out <tt><b>FIRE!</b></tt> and end of program.
</ul>
<p>
We must consider that the loop has to end at some point, therefore, within the block
of instructions (loop's <tt><i>statement</i></tt>) we must provide some method that
forces <tt><i>condition</i></tt> to become false at some moment, otherwise the loop will
continue looping forever.
In this case we have included <tt><b>--n;</b></tt> that causes the <TT><I>condition</I></TT>
to become <TT><B>false</B></TT> after some loop repetitions: when <TT><B>n</B></TT> becomes
<TT><B>0</B></TT>, that is where our countdown ends.
<p>
Of course this is such a simple action for our computer that the whole countdown is
performed instantly without practical delay between numbers.
<P>
<dt><b>The <i>do-while</i> loop.</b><br>
<dd>
Format:<br>
<blockquote>
<tt><b>do </b><i>statement</i><b> while (</b><i>condition</i><b>);</b></tt><br>
</blockquote>
Its functionality is exactly the same as the <i>while</i> loop except that
<tt><i>condition</i></tt> in the <I>do-while</I> is evaluated <U>after</U> the execution
of <tt><i>statement</i></tt> instead of before, granting at least one execution of
<tt><i>statement</i></tt> even if <tt><i>condition</i></tt> is never
fulfilled. For example, the following program echoes any number you enter until
you enter 0.
<P><CENTER>
<TABLE WIDTH=90% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// number echoer</I>
#include &lt;iostream.h&gt;
int main ()
{
  unsigned long n;
  do {
    cout &lt;&lt; "Enter number (0 to end): ";
    cin &gt;&gt; n;
    cout &lt;&lt; "You entered: " &lt;&lt; n &lt;&lt; "\n";
  } while (n != 0);
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top"><TT>
<B>Enter number (0 to end): </B>12345<BR>
<B>You entered: 12345</B><BR>
<B>Enter number (0 to end): </B>160277<BR>
<B>You entered: 160277</B><BR>
<B>Enter number (0 to end): </B>0<BR>
<B>You entered: 0</B><BR>
</TT></TD></TR></TABLE>
</CENTER>
<P>
The <I>do-while</I> loop is usually used when the condition that has to determine its end
is determined within the loop statement, like in the previous case, where the user input
within the block of intructions is what determines the end of the loop. If you never enter
the <TT><B>0</B></TT> value in the previous example the loop will never end.

<p>
<dt><b>The <I>for</I> loop.</b><br>
<dd>
Its format is:<br>
<blockquote>
<tt><b>for (</b><i>initialization</i><b>; </b><i>condition</i><b>; </b><i>increase</i><b>) </b><i>statement</i>;</tt><br>
</blockquote>
and its main function is to repeat <tt><i>statement</i></tt> while
<tt><i>condition</i></tt> remains true, like the <I>while</I> loop. But in addition,
<TT><B>for</B></TT> provides places to specify an <TT><I>initialization</I></TT> instruction
and an <TT><I>increase</I></TT> instruction. So this loop is specially designed to
perform a repetitive action with a counter.
<p>
It works the following way:<br>
<blockquote>
1,  <TT><I>initialization</I></TT> is executed. Generally it is an initial value setting for a counter varible. This is executed only <U>once</U>.<br>
2,  <tt><i>condition</i></tt> is checked, if it is <TT><B>true</B></TT> the loop continues, otherwise the loop finishes and <TT><I>statement</I></TT> is skipped.<br>
3,  <tt><i>statement</i></tt> is executed. As usual, it can be either a single instruction or a block of instructions enclosed within curly brackets <tt><b>{ }</b></tt>.<br>
4,  finally, whatever is specified in the <tt><i>increase</i></tt> field is executed and the loop gets back to step 2.<br>
</blockquote>
Here is an example of countdown using a <I>for</I> loop.
<P><CENTER>
<TABLE WIDTH=90% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// countdown using a for loop</I>
#include &lt;iostream.h&gt;
int main ()
{
  for (int n=10; n&gt;0; n--) {
    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, 5, 4, 3, 2, 1, FIRE! </TT></B>
</TD></TR></TABLE>
</CENTER>
<P>
The <tt><i>initialization</i></tt> and <tt><i>increase</i></tt> fields
are optional. They can be avoided but not the semicolon signs among them. For example
we could write: <b><tt>for (;n&lt;10;)</tt></b> if we want to specify no
<TT><I>initialization</I></TT> and no <TT><I>increase</I></TT>; or
<b><tt>for (;n&lt;10;n++)</tt></b>
if we want to include an <TT><I>increase</I></TT> field but not an <TT><I>initialization</I></TT>.

<P>
Optionally, using the comma operator (<TT><B>,</B></TT>) we can specify more than one

⌨️ 快捷键说明

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