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

📄 ch07.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><!-- This document was created from RTF source by rtftohtml version 3.0.1 -->	<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">	<TITLE>Teach Yourself C++ in 21 Days</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><A HREF="ch06.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch06.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMGSRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch07rv1.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch07rv1.htm"><IMG SRC="BLANNEXT.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1><H1></H1><UL>	<LI><A HREF="#Heading1">Day 7</A>	<UL>		<LI><A HREF="#Heading2">More Program Flow</A>		<UL>			<LI><A HREF="#Heading3">Looping</A>			<UL>				<LI><A HREF="#Heading4">The Roots of Looping goto</A>			</UL>			<LI><A HREF="#Heading5">Listing 7.1. Looping with the keyword goto</A><A HREF="#Heading6">.</A>			<UL>				<LI><A HREF="#Heading7">Why goto Is Shunned</A>			</UL>			<LI><A HREF="#Heading8">The goto Statement</A>			<LI><A HREF="#Heading9">while Loops</A>			<LI><A HREF="#Heading10">Listing 7.2. while loops</A><A HREF="#Heading11">.</A>			<LI><A HREF="#Heading12">The while Statement</A>			<UL>				<LI><A HREF="#Heading13">More Complicated while Statements</A>			</UL>			<LI><A HREF="#Heading14">Listing 7.3. Complex while loops</A><A HREF="#Heading15">.</A>			<UL>				<LI><A HREF="#Heading16">continue and break</A>			</UL>			<LI><A HREF="#Heading17">Listing 7.4. break and continue</A><A HREF="#Heading18">.</A>			<LI><A HREF="#Heading19">The continue Statement</A>			<LI><A HREF="#Heading20">The break Statement</A>			<UL>				<LI><A HREF="#Heading21">while (1) Loops</A>			</UL>			<LI><A HREF="#Heading22">Listing 7.5. while (1) loops</A><A HREF="#Heading23">.</A>			<LI><A HREF="#Heading24">do...while Loops</A>			<LI><A HREF="#Heading25">Listing 7.6. Skipping the body of the while Loop</A><A HREF="#Heading26">.</A>			<LI><A HREF="#Heading27">do...while</A>			<LI><A HREF="#Heading28">Listing 7.7. Demonstrates do...while loop</A><A HREF="#Heading29">.</A>			<LI><A HREF="#Heading30">The do...while Statement</A>			<LI><A HREF="#Heading31">for Loops</A>			<LI><A HREF="#Heading32">Listing 7.8. While reexamined</A><A HREF="#Heading33">.</A>			<LI><A HREF="#Heading34">Listing 7.9. Demonstrating the for loop</A><A HREF="#Heading35">.</A>			<LI><A HREF="#Heading36">The for Statement</A>			<UL>				<LI><A HREF="#Heading37">Advanced for Loops</A>			</UL>			<LI><A HREF="#Heading38">Listing 7.10. Demonstrating multiple statements in for loops</A><A			HREF="#Heading39">.</A>			<LI><A HREF="#Heading40">Listing 7.11. Null statements in for loops</A><A HREF="#Heading41">.</A>			<LI><A HREF="#Heading42">Listing 7.12. Illustrating empty for loop statement</A><A			HREF="#Heading43">.</A>			<UL>				<LI><A HREF="#Heading44">Empty for Loops</A>			</UL>			<LI><A HREF="#Heading45">Listing 7.13. Illustrates the null statement in a for loop</A><A			HREF="#Heading46">.</A>			<UL>				<LI><A HREF="#Heading47">Nested Loops</A>			</UL>			<LI><A HREF="#Heading48">Listing 7.14. Illustrates nested for loops</A><A HREF="#Heading49">.</A>			<UL>				<LI><A HREF="#Heading50">Scoping in for Loops</A>			</UL>			<LI><A HREF="#Heading51">Summing Up Loops</A>			<LI><A HREF="#Heading52">Listing 7.15. Solving the nth Fibonacci number</A>			<LI><A HREF="#Heading53">using iteration.</A>			<LI><A HREF="#Heading54">switch Statements</A>			<LI><A HREF="#Heading55">Listing 7.16. Demonstrating the switch statement</A><A HREF="#Heading56">.</A>			<LI><A HREF="#Heading57">The switch Statement</A>			<UL>				<LI><A HREF="#Heading58">Using a switch Statement with a Menu</A>			</UL>			<LI><A HREF="#Heading59">Listing 7.17. Demonstrating a forever loop</A><A HREF="#Heading60">.</A>			<LI><A HREF="#Heading61">Summary</A>			<LI><A HREF="#Heading62">Q&amp;A</A>			<LI><A HREF="#Heading63">Workshop</A>			<UL>				<LI><A HREF="#Heading64">Quiz</A>				<LI><A HREF="#Heading65">Exercises</A>			</UL>		</UL>	</UL></UL><P><HR SIZE="4"><H2 ALIGN="CENTER"><BR><A NAME="Heading1"></A><FONT COLOR="#000077">Day 7</FONT></H2><H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">More Program Flow</FONT></H2><P>Programs accomplish most of their work by branching and looping. On Day 4, &quot;Expressionsand Statements,&quot; you learned how to branch your program using the <TT>if</TT>statement. Today you learn<UL>	<LI>What loops are and how they are used.	<P>	<LI>How to build various loops.	<P>	<LI>An alternative to deeply-nested <TT>if/else</TT> statements.</UL><H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Looping</FONT></H3><P>Many programming problems are solved by repeatedly acting on the same data. Thereare two ways to do this: recursion (discussed yesterday) and iteration. Iterationmeans doing the same thing again and again. The principal method of iteration isthe loop.<H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">The Roots of Loopinggoto</FONT></H4><P>In the primitive days of early computer science, programs were nasty, brutish,and short. Loops consisted of a label, some statements, and a jump.</P><P>In C++, a label is just a name followed by a colon (<TT>:</TT>). The label isplaced to the left of a legal C++ statement, and a jump is accomplished by writing<TT>goto</TT> followed by the label name. Listing 7.1 illustrates this.</P><P><A NAME="Heading5"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 7.1. Loopingwith the keyword goto.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:    // Listing 7.12:    // Looping with goto3:4:    #include &lt;iostream.h&gt;5:6:    int main()7:    {8:           int counter = 0;      // initialize counter9:    loop:  counter ++;           // top of the loop10:            cout &lt;&lt; &quot;counter: &quot; &lt;&lt; counter &lt;&lt; &quot;\n&quot;;11:           if (counter &lt; 5)            // test the value12:               goto loop;                 // jump to the top13:14:           cout &lt;&lt; &quot;Complete. Counter: &quot; &lt;&lt; counter &lt;&lt; &quot;.\n&quot;;15:       return 0;<TT>16: }</TT>Output: counter: 1counter: 2counter: 3counter: 4counter: 5Complete. Counter: 5.</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>On line 8, <TT>counter</TT>is initialized to <TT>0</TT>. The label <TT>loop</TT> is on line 9, marking the topof the loop. <TT>Counter</TT> is incremented and its new value is printed. The valueof <TT>counter</TT> is tested on line 11. If it is less than 5, the <TT>if</TT> statementis true and the <TT>goto</TT> statement is executed. This causes program executionto jump back to line 9. The program continues looping until <TT>counter</TT> is equalto 5, at which time it &quot;falls through&quot; the loop and the final output isprinted.<H4 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077">Why goto Is Shunned</FONT></H4><P><TT>goto</TT> has received some rotten press lately, and it's well deserved. <TT>goto</TT>statements can cause a jump to any location in your source code, backward or forward.The indiscriminate use of <TT>goto</TT> statements has caused tangled, miserable,impossible-to-read programs known as &quot;spaghetti code.&quot; Because of this,computer science teachers have spent the past 20 years drumming one lesson into theheads of their students: &quot;Never, ever, ever use <TT>goto</TT>! It is evil!&quot;</P><P>To avoid the use of <TT>goto</TT>, more sophisticated, tightly controlled loopingcommands have been introduced: <TT>for</TT>, <TT>while</TT>, and <TT>do...while</TT>.Using these makes programs that are more easily understood, and <TT>goto</TT> isgenerally avoided, but one might argue that the case has been a bit overstated. Likeany tool, carefully used and in the right hands, <TT>goto</TT> can be a useful construct,and the ANSI committee decided to keep it in the language because it has its legitimateuses. But as they say, kids, don't try this at home.<H3 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">The goto Statement</FONT></H3><P>To use the <TT>goto</TT> statement, you write <TT>goto</TT> followed by a labelname. This causes an unconditioned jump to the label. Example</P><PRE><FONT COLOR="#0066FF">if (value &gt; 10)     goto end;if (value &lt; 10)     goto end;cout &lt;&lt; &quot;value is &#194;10!&quot;;end:cout &lt;&lt; &quot;done&quot;;</FONT></PRE><BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>Use of <TT>goto</TT> is almost	always a sign of bad design. The best advice is to avoid using it. In 10 years of	programming, I've needed it only once. <HR></BLOCKQUOTE><H3 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">while Loops</FONT></H3><P>A <TT>while</TT> loop causes your program to repeat a sequence of statements aslong as the starting condition remains true. In the example of <TT>goto</TT>, inListing 7.1, the counter was incremented until it was equal to 5. Listing 7.2 showsthe same program rewritten to take advantage of a <TT>while</TT> loop.</P><P><A NAME="Heading10"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 7.2. while loops.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:    // Listing 7.22:    // Looping with while3:4:    #include &lt;iostream.h&gt;5:6:    int main()7:    {8:      int counter = 0;               // initialize the condition9:10:      while(counter &lt; 5)     // test condition still true11:       {12:          counter++;              // body of the loop13:          cout &lt;&lt; &quot;counter: &quot; &lt;&lt; counter &lt;&lt; &quot;\n&quot;;14:      }15:16:      cout &lt;&lt; &quot;Complete. Counter: &quot; &lt;&lt; counter &lt;&lt; &quot;.\n&quot;;17:       return 0;<TT>18: }</TT>Output: counter: 1counter: 2counter: 3counter: 4counter: 5Complete. Counter: 5.</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>This simple program demonstratesthe fundamentals of the <TT>while</TT> loop. A condition is tested, and if it istrue, the body of the <TT>while</TT> loop is executed. In this case, the conditiontested on line 10 is whether <TT>counter</TT> is less than 5. If the condition istrue, the body of the loop is executed; on line 12 the counter is incremented, andon line 13 the value is printed. When the conditional statement on line 10 fails(when <TT>counter</TT> is no longer less than 5), the entire body of the <TT>while</TT>loop (lines 11-14) is skipped. Program execution falls through to line 15.<H3 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">The while Statement</FONT></H3><P>The syntax for the <TT>while</TT> statement is as follows:</P><PRE><FONT COLOR="#0066FF">while ( condition )statement;</FONT></PRE><P>condition is any C++ expression, and statement is any valid C++ statement or blockof statements. When condition evaluates to <TT>TRUE</TT> (<TT>1</TT>), statementis executed, and then condition is tested again. This continues until condition tests<TT>FALSE</TT>, at which time the <TT>while</TT> loop terminates and execution continueson the first line below statement.<BR><BR>Example</P><PRE><FONT COLOR="#0066FF">// count to 10int x = 0;while (x &lt; 10)cout &lt;&lt; &quot;X: &quot; &lt;&lt; x++;</FONT></PRE><H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">More Complicatedwhile Statements</FONT></H4><P>The condition tested by a <TT>while</TT> loop can be as complex as any legal C++expression. This can include expressions produced using the logical <TT>&amp;&amp;</TT>(AND), <TT>||</TT> (OR), and <TT>!</TT> (NOT) operators. Listing 7.3 is a somewhatmore complicated <TT>while</TT> statement.</P><P><A NAME="Heading14"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 7.3. Complexwhile loops.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:    // Listing 7.32:    // Complex while statements3:4:    #include &lt;iostream.h&gt;5:6:    int main()7:    {8:      unsigned short small;9:      unsigned long  large;10:      const unsigned short MAXSMALL=65535;11:12:      cout &lt;&lt; &quot;Enter a small number: &quot;;13:      cin &gt;&gt; small;14:      cout &lt;&lt; &quot;Enter a large number: &quot;;15:      cin &gt;&gt; large;16:17:       cout &lt;&lt; &quot;small: &quot; &lt;&lt; small &lt;&lt; &quot;...&quot;;18:19:      // for each iteration, test three conditions20:      while (small &lt; large &amp;&amp; large &gt; 0 &amp;&amp; small &lt; MAXSMALL)21:22:      {23:         if (small % 5000 == 0)  // write a dot every 5k lines24:           cout &lt;&lt; &quot;.&quot;;25:26:         small++;27:28:         large-=2;29:      }30:31:      cout &lt;&lt; &quot;\nSmall: &quot; &lt;&lt; small &lt;&lt; &quot; Large: &quot; &lt;&lt; large &lt;&lt; endl;32:     return 0;<TT>33: }</TT>Output: Enter a small number: 2Enter a large number: 100000small: 2.........Small: 33335 Large: 33334</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>This program is a game. Entertwo numbers, one small and one large. The smaller number will count up by ones, andthe larger number will count down by twos. The goal of the game is to guess whenthey'll meet.<BR>On lines 12-15, the numbers are entered. Line 20 sets up a <TT>while</TT> loop, whichwill continue only as long as three conditions are met:<BR><BR>

⌨️ 快捷键说明

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