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

📄 chapter03.html

📁 Thinking in c++ 2nd edition,c++编程思想(第2版)
💻 HTML
📖 第 1 页 / 共 5 页
字号:
  <font color=#0000ff>if</font>(i == 1)
    <font color=#0000ff>return</font> 'g';
  <font color=#0000ff>if</font>(i == 5)
    <font color=#0000ff>return</font> 'z';
  <font color=#0000ff>return</font> 'c';
}

<font color=#0000ff>int</font> main() {
  cout &lt;&lt; <font color=#004488>"type an integer: "</font>;
  <font color=#0000ff>int</font> val;
  cin &gt;&gt; val;
  cout &lt;&lt; cfunc(val) &lt;&lt; endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>cfunc(&#160;)</B>, the first
<B>if</B> that evaluates to <B>true</B> exits the function via the <B>return</B>
statement. Notice that a function declaration is not necessary because the
function definition appears before it is used in <B>main(&#160;)</B>, so the
compiler knows about it from that function
definition.</FONT><A NAME="_Toc462979739"></A><A NAME="_Toc472654744"></A><BR></P></DIV>
<A NAME="Heading102"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Using the C function library<A NAME="Index449"></A><A NAME="Index450"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All the functions in your local C
function library are available while you are programming in C++. You should look
hard at the function library before defining your own function &#8211;
there&#8217;s a good chance that someone has already solved your problem for
you, and probably given it a lot more thought and debugging.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A word of caution, though: many compilers
include a lot of extra functions that make life even easier and are tempting to
use, but are not part of the Standard C library. If you are certain you will
never want to move the application to another platform (and who is certain of
that?), go ahead &#8211;use those functions and make your life easier. If you
want your application to be portable, you should restrict yourself to Standard
library functions. If you must perform platform-specific activities, try to
isolate that code in one spot so it can be changed easily when porting to
another platform. In C++, platform-specific activities are often encapsulated in
a class, which is the ideal solution.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The formula for using a library function
is as follows: first, find the function in your programming reference (many
programming references will index the function by category as well as
alphabetically). The description of the function should include a section that
demonstrates the syntax of the code. The top of this section usually has at
least one <B>#include</B> line, showing you the header file containing the
function prototype. Duplicate this <B>#include</B> line in your file so the
function is properly
declared<A NAME="Index451"></A><A NAME="Index452"></A><A NAME="Index453"></A>.
Now you can call the function in the same way it appears in the syntax section.
If you make a mistake, the compiler will discover it by comparing your function
call to the function prototype in the header and tell you about your error. The
linker searches the Standard library by default, so that&#8217;s all you need to
do: include the header file and call the
function.</FONT><A NAME="_Toc462979740"></A><A NAME="_Toc472654745"></A><BR></P></DIV>
<A NAME="Heading103"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Creating your own libraries with the librarian <A NAME="Index454"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can collect your own functions
together into a library. Most programming packages come with a librarian that
manages groups of object modules. Each librarian has its own commands, but the
general idea is this: if you want to create a library, make a header file
containing the function prototypes for all the functions in your library. Put
this header file somewhere in the preprocessor&#8217;s search path, either in
the local directory (so it can be found by <B>#include "header"</B>) or in the
include directory (so it can be found by <B>#include &lt;header&gt;</B>). Now
take all the object modules and hand them to the librarian along with a name for
the finished library (most librarians require a common extension, such as
<B>.lib</B> or <B>.a</B>). Place the finished library where the other libraries
reside so the linker can find it. When you use your library, you will have to
add something to the command line so the linker <A NAME="Index455"></A>knows to
search the library for the functions you call. You must find all the details in
your local manual, since they vary from system to
system.</FONT><A NAME="_Toc462979741"></A><A NAME="_Toc472654746"></A><BR></P></DIV>
<A NAME="Heading104"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Controlling execution <A NAME="Index456"></A><A NAME="Index457"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This section covers the execution control
statements in C++. You must be familiar with these statements before you can
read and write C or C++ code.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C++ uses all of C&#8217;s execution
control statements. These include <B>if-else</B>, <B>while</B>, <B>do-while</B>,
<B>for</B>, and a selection statement called <B>switch</B>. C++ also allows the
infamous <B>goto</B>, which will be avoided in this
book.</FONT><A NAME="_Toc462979742"></A><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index458"></A><A NAME="Index459"></A><A NAME="_Toc472654747"></A><BR></P></DIV>
<A NAME="Heading105"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
True and false<BR><A NAME="Index460"></A><A NAME="Index461"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All conditional statements use the truth
or falsehood of a conditional expression to determine the execution path. An
example of a conditional expression is <B>A == B</B>. This uses the conditional
operator <B>==</B> to see if the variable <B>A</B> is equivalent to the variable
<B>B.</B> The expression produces a <A NAME="Index462"></A>Boolean <B>true
</B>or <B>false</B> (these are keywords only in C++; in C an expression is
&#8220;true&#8221; if it evaluates to a nonzero value). Other conditional
operators are <B>&gt;</B>, <B>&lt;</B>, <B>&gt;=</B>, etc. Conditional
statements are covered more fully later in this chapter.
</FONT><A NAME="_Toc462979743"></A><A NAME="_Toc472654748"></A><BR></P></DIV>
<A NAME="Heading106"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
if-else<A NAME="Index463"></A><A NAME="Index464"></A><BR><A NAME="Index465"></A><A NAME="Index466"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>if-else</B> statement can exist in
two forms: with or without the <B>else</B>. The two forms are:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>if</font>(expression)
    statement</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">or</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>if</font>(expression)
    statement
<font color=#0000ff>else</font>
    statement</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The &#8220;expression&#8221; evaluates to
<B>true</B> or <B>false</B>. The &#8220;statement&#8221; means either a simple
statement terminated by a semicolon or a compound statement, which is a group of
simple statements enclosed in braces. Any time the word &#8220;statement&#8221;
is used, it always implies that the statement is simple or compound. Note that
this statement can also be another <B>if</B>, so they can be strung
together.</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Ifthen.cpp</font>
<font color=#009900>// Demonstration of if and if-else conditionals</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>int</font> main() {
  <font color=#0000ff>int</font> i;
  cout &lt;&lt; <font color=#004488>"type a number and 'Enter'"</font> &lt;&lt; endl;
  cin &gt;&gt; i;
  <font color=#0000ff>if</font>(i &gt; 5)
    cout &lt;&lt; <font color=#004488>"It's greater than 5"</font> &lt;&lt; endl;
  <font color=#0000ff>else</font>
    <font color=#0000ff>if</font>(i &lt; 5)
      cout &lt;&lt; <font color=#004488>"It's less than 5 "</font> &lt;&lt; endl;
    <font color=#0000ff>else</font>
      cout &lt;&lt; <font color=#004488>"It's equal to 5 "</font> &lt;&lt; endl;

  cout &lt;&lt; <font color=#004488>"type a number and 'Enter'"</font> &lt;&lt; endl;
  cin &gt;&gt; i;
  <font color=#0000ff>if</font>(i &lt; 10)
    <font color=#0000ff>if</font>(i &gt; 5)  <font color=#009900>// "if" is just another statement</font>
      cout &lt;&lt; <font color=#004488>"5 &lt; i &lt; 10"</font> &lt;&lt; endl;
    <font color=#0000ff>else</font>
      cout &lt;&lt; <font color=#004488>"i &lt;= 5"</font> &lt;&lt; endl;
  <font color=#0000ff>else</font> <font color=#009900>// Matches "if(i &lt; 10)"</font>
    cout &lt;&lt; <font color=#004488>"i &gt;= 10"</font> &lt;&lt; endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It is conventional to indent the body of
a control flow statement so the reader may easily determine where it begins and
ends</FONT><A NAME="fnB30" HREF="#fn30">[30]</A><FONT FACE="Georgia">.</FONT><A NAME="_Toc462979744"></A><A NAME="_Toc472654749"></A><BR></P></DIV>
<A NAME="Heading107"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
while<A NAME="Index467"></A><A NAME="Index468"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>while</B>, <B>do-while,</B> and
<B>for</B> control looping. A statement repeats until the controlling expression
evaluates to <B>false</B>. The form of a <B>while</B> loop is</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>while</font>(expression)
    statement</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The expression is evaluated once at the
beginning of the loop and again before each further iteration of the
statement.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This example stays in the body of the
<B>while</B> loop until you type the secret number or press
control-C.</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Guess.cpp</font>
<font color=#009900>// Guess a number (demonstrates "while")</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>int</font> main() {
  <font color=#0000ff>int</font> secret = 15;
  <font color=#0000ff>int</font> guess = 0;
  <font color=#009900>// "!=" is the "not-equal" conditional:</font>
  <font color=#0000ff>while</font>(guess != secret) { <font color=#009900>// Compound statement</font>
    cout &lt;&lt; <font color=#004488>"guess the number: "</font>;
    cin &gt;&gt; guess;
  }
  cout &lt;&lt; <font color=#004488>"You guessed it!"</font> &lt;&lt; endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>while</B>&#8217;s<B>
</B>conditional expression is not restricted to a simple test as in the example
above; it can be as complicated as you like as long as it produces a <B>true</B>
or <B>false</B> result. You will even see code where the loop has no body, just
a bare semicolon:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>while</font>(<font color=#009900>/* Do a lot here */</font>)
 ;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In these cases, the programmer has
written the conditional expression not only to perform the test but also to do
the
work.</FONT><A NAME="_Toc462979745"></A><A NAME="_Toc472654750"></A><BR></P></DIV>
<A NAME="Heading108"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
do-while<A NAME="Index469"></A><A NAME="Index470"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The form of <B>do-while</B>
is</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>do</font>
    statement
<B><font color=#0000ff>while</font>(</B>expression<B>);</B> </PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>do-while</B> is different from the
while because the statement always executes at least once, even if the
expression evaluates to false the first time. In a regular <B>while</B>, if the
conditional is false the first time the statement never
executes.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If a <B>do-while</B> is used in
<B>Guess.cpp</B>, the variable <B>guess</B> does not need an initial dummy
value, since it is initialized by the <B>cin</B> statement before it is
tested:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Guess2.cpp</font>
<font color=#009900>// The guess program using do-while</font>

⌨️ 快捷键说明

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