?? ch07.htm
字號(hào):
<!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://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch06.htm"><IMG SRC="../buttons/BLANPREV.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="javascript:if(confirm('http://www.mcp.com/sams \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mcp.com/sams'" tppabs="http://www.mcp.com/sams"><IMG
SRC="../buttons/BLANHOME.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="../index.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/index.htm"><IMG SRC="../buttons/BLANTOC.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch07rv1.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch07rv1.htm"><IMG SRC="../buttons/BLANNEXT.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/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&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, "Expressions
and Statements," 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. There
are two ways to do this: recursion (discussed yesterday) and iteration. Iteration
means doing the same thing again and again. The principal method of iteration is
the loop.
<H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">The Roots of Looping
goto</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 is
placed 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. Looping
with the keyword goto.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // Listing 7.1
2: // Looping with goto
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: int counter = 0; // initialize counter
9: loop: counter ++; // top of the loop
10: cout << "counter: " << counter << "\n";
11: if (counter < 5) // test the value
12: goto loop; // jump to the top
13:
14: cout << "Complete. Counter: " << counter << ".\n";
15: return 0;
<TT>16: }</TT>
Output: counter: 1
counter: 2
counter: 3
counter: 4
counter: 5
Complete. 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 top
of the loop. <TT>Counter</TT> is incremented and its new value is printed. The value
of <TT>counter</TT> is tested on line 11. If it is less than 5, the <TT>if</TT> statement
is true and the <TT>goto</TT> statement is executed. This causes program execution
to jump back to line 9. The program continues looping until <TT>counter</TT> is equal
to 5, at which time it "falls through" the loop and the final output is
printed.
<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 "spaghetti code." Because of this,
computer science teachers have spent the past 20 years drumming one lesson into the
heads of their students: "Never, ever, ever use <TT>goto</TT>! It is evil!"</P>
<P>To avoid the use of <TT>goto</TT>, more sophisticated, tightly controlled looping
commands 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> is
generally avoided, but one might argue that the case has been a bit overstated. Like
any 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 legitimate
uses. 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 label
name. This causes an unconditioned jump to the label. Example</P>
<PRE><FONT COLOR="#0066FF">if (value > 10) goto end;if (value < 10) goto end;cout << "value is Â10!";end:cout << "done";
</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 as
long as the starting condition remains true. In the example of <TT>goto</TT>, in
Listing 7.1, the counter was incremented until it was equal to 5. Listing 7.2 shows
the 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.2
2: // Looping with while
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: int counter = 0; // initialize the condition
9:
10: while(counter < 5) // test condition still true
11: {
12: counter++; // body of the loop
13: cout << "counter: " << counter << "\n";
14: }
15:
16: cout << "Complete. Counter: " << counter << ".\n";
17: return 0;
<TT>18: }</TT>
Output: counter: 1
counter: 2
counter: 3
counter: 4
counter: 5
Complete. Counter: 5.
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>This simple program demonstrates
the fundamentals of the <TT>while</TT> loop. A condition is tested, and if it is
true, the body of the <TT>while</TT> loop is executed. In this case, the condition
tested on line 10 is whether <TT>counter</TT> is less than 5. If the condition is
true, the body of the loop is executed; on line 12 the counter is incremented, and
on 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 block
of statements. When condition evaluates to <TT>TRUE</TT> (<TT>1</TT>), statement
is 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 continues
on the first line below statement.<BR>
<BR>
Example</P>
<PRE><FONT COLOR="#0066FF">// count to 10
int x = 0;
while (x < 10)
cout << "X: " << x++;
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">More Complicated
while 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>&&</TT>
(AND), <TT>||</TT> (OR), and <TT>!</TT> (NOT) operators. Listing 7.3 is a somewhat
more complicated <TT>while</TT> statement.</P>
<P><A NAME="Heading14"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 7.3. Complex
while loops.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // Listing 7.3
2: // Complex while statements
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: unsigned short small;
9: unsigned long large;
10: const unsigned short MAXSMALL=65535;
11:
12: cout << "Enter a small number: ";
13: cin >> small;
14: cout << "Enter a large number: ";
15: cin >> large;
16:
17: cout << "small: " << small << "...";
18:
19: // for each iteration, test three conditions
20: while (small < large && large > 0 && small < MAXSMALL)
21:
22: {
23: if (small % 5000 == 0) // write a dot every 5k lines
24: cout << ".";
25:
26: small++;
27:
28: large-=2;
29: }
30:
31: cout << "\nSmall: " << small << " Large: " << large << endl;
32: return 0;
<TT>33: }</TT>
Output: Enter a small number: 2
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -