📄 chapter03.html
字号:
#include <iostream>
<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; <font color=#009900>// No initialization needed here</font>
<font color=#0000ff>do</font> {
cout << <font color=#004488>"guess the number: "</font>;
cin >> guess; <font color=#009900>// Initialization happens</font>
} <font color=#0000ff>while</font>(guess != secret);
cout << <font color=#004488>"You got it!"</font> << endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For some reason, most programmers tend to
avoid <B>do-while</B> and just work with
<B>while</B>.</FONT><A NAME="_Toc462979746"></A><A NAME="_Toc472654751"></A><BR></P></DIV>
<A NAME="Heading109"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
for<A NAME="Index471"></A><A NAME="Index472"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>for</B> loop performs initialization
before the first iteration. Then it performs conditional testing and, at the end
of each iteration, some form of “stepping.” The form of the
<B>for</B> loop is:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>for</font>(initialization; conditional; step)
<B> statement</B></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Any of the expressions
<A NAME="Index473"></A><I>initialization</I>,
<A NAME="Index474"></A><I>conditional,</I> or <A NAME="Index475"></A><I>step</I>
may be empty. The <I>initialization</I> code executes once at the very
beginning. The <I>conditional</I> is tested before each iteration (if it
evaluates to false at the beginning, the statement never executes). At the end
of each loop, the <I>step</I> executes.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>for</B> loops are usually used for
“counting” tasks:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Charlist.cpp</font>
<font color=#009900>// Display all the ASCII characters</font>
<font color=#009900>// Demonstrates "for"</font>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < 128; i = i + 1)
<font color=#0000ff>if</font> (i != 26) <font color=#009900>// ANSI Terminal Clear screen</font>
cout << <font color=#004488>" value: "</font> << i
<< <font color=#004488>" character: "</font>
<< <font color=#0000ff>char</font>(i) <font color=#009900>// Type conversion</font>
<< endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You may notice that the variable <B>i</B>
is defined at the point where it is used, instead of at the beginning of the
block denoted by the open curly brace ‘<B>{</B>’. This is in
contrast to traditional procedural languages (including C), which require that
all variables be defined at the beginning of the block. This will be discussed
later in this
chapter.</FONT><A NAME="_Toc462979747"></A><A NAME="_Toc472654752"></A><BR></P></DIV>
<A NAME="Heading110"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The break<A NAME="Index476"></A> and continue <A NAME="Index477"></A>keywords
</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Inside the body of any of the looping
constructs <B>while</B>, <B>do-while,</B> or <B>for</B>,<B> </B>you can control
the flow of the loop using <A NAME="Index478"></A><B>break</B> and
<A NAME="Index479"></A><B>continue</B>. <B>break</B> quits the loop without
executing the rest of the statements in the loop. <B>continue</B> stops the
execution of the current iteration and goes back to the beginning of the loop to
begin a new iteration.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As an example of <B>break</B> and
<B>continue</B>, this program is a very simple menu system:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Menu.cpp</font>
<font color=#009900>// Simple menu program demonstrating</font>
<font color=#009900>// the use of "break" and "continue"</font>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
<font color=#0000ff>char</font> c; <font color=#009900>// To hold response</font>
<font color=#0000ff>while</font>(<font color=#0000ff>true</font>) {
cout << <font color=#004488>"MAIN MENU:"</font> << endl;
cout << <font color=#004488>"l: left, r: right, q: quit -> "</font>;
cin >> c;
<font color=#0000ff>if</font>(c == 'q')
<font color=#0000ff>break</font>; <font color=#009900>// Out of "while(1)"</font>
<font color=#0000ff>if</font>(c == 'l') {
cout << <font color=#004488>"LEFT MENU:"</font> << endl;
cout << <font color=#004488>"select a or b: "</font>;
cin >> c;
<font color=#0000ff>if</font>(c == 'a') {
cout << <font color=#004488>"you chose 'a'"</font> << endl;
<font color=#0000ff>continue</font>; <font color=#009900>// Back to main menu</font>
}
<font color=#0000ff>if</font>(c == 'b') {
cout << <font color=#004488>"you chose 'b'"</font> << endl;
<font color=#0000ff>continue</font>; <font color=#009900>// Back to main menu</font>
}
<font color=#0000ff>else</font> {
cout << <font color=#004488>"you didn't choose a or b!"</font>
<< endl;
<font color=#0000ff>continue</font>; <font color=#009900>// Back to main menu</font>
}
}
<font color=#0000ff>if</font>(c == 'r') {
cout << <font color=#004488>"RIGHT MENU:"</font> << endl;
cout << <font color=#004488>"select c or d: "</font>;
cin >> c;
<font color=#0000ff>if</font>(c == 'c') {
cout << <font color=#004488>"you chose 'c'"</font> << endl;
<font color=#0000ff>continue</font>; <font color=#009900>// Back to main menu</font>
}
<font color=#0000ff>if</font>(c == 'd') {
cout << <font color=#004488>"you chose 'd'"</font> << endl;
<font color=#0000ff>continue</font>; <font color=#009900>// Back to main menu</font>
}
<font color=#0000ff>else</font> {
cout << <font color=#004488>"you didn't choose c or d!"</font>
<< endl;
<font color=#0000ff>continue</font>; <font color=#009900>// Back to main menu</font>
}
}
cout << <font color=#004488>"you must type l or r or q!"</font> << endl;
}
cout << <font color=#004488>"quitting menu..."</font> << endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If the user selects ‘q’ in
the main menu, the <B>break</B> keyword is used to quit, otherwise the program
just continues to execute indefinitely. After each of the sub-menu selections,
the <B>continue</B> keyword is used to pop back up to the beginning of the while
loop.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>while(true)</B> statement is the
equivalent of saying “do this loop forever.” The <B>break</B>
statement allows you to break out of this infinite while loop when the user
types a ‘q.’
</FONT><A NAME="_Toc462979748"></A><A NAME="_Toc472654753"></A><BR></P></DIV>
<A NAME="Heading111"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
switch<A NAME="Index480"></A><A NAME="Index481"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>switch</B> statement selects from
among pieces of code based on the value of an integral expression. Its form
is:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>switch</font>(selector) {
<font color=#0000ff>case</font> integral-value1 : statement; <font color=#0000ff>break</font>;
<font color=#0000ff>case</font> integral-value2 : statement; <font color=#0000ff>break</font>;
<font color=#0000ff>case</font> integral-value3 : statement; <font color=#0000ff>break</font>;
<font color=#0000ff>case</font> integral-value4 : statement; <font color=#0000ff>break</font>;
<font color=#0000ff>case</font> integral-value5 : statement; <font color=#0000ff>break</font>;
(...)
<font color=#0000ff>default</font>: statement;
<B>}</B> </PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><I>Selector</I> is an expression that
produces an integral value. The <B>switch</B> compares the result of
<I>selector</I> to each <I>integral value</I>. If it finds a match, the
corresponding statement (simple or compound) executes. If no match occurs, the
<B>default</B> <A NAME="Index482"></A><A NAME="Index483"></A>statement
executes.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You will notice in the definition above
that each <B>case<A NAME="Index484"></A><A NAME="Index485"></A></B> ends with a
<B>break</B>, which causes execution to jump to the end of the <B>switch</B>
body (the closing brace that completes the <B>switch</B>). This is the
conventional way to build a <B>switch</B> statement, but the <B>break</B> is
optional. If it is missing, your <B>case </B>“drops through” to the
one after it. That is, the code for the following <B>case</B> statements execute
until a <B>break</B> is encountered. Although you don’t usually want this
kind of behavior, it can be useful to an experienced
programmer.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>switch</B> statement is a clean
way to implement <A NAME="Index486"></A><A NAME="Index487"></A>multi-way
selection (i.e., selecting from among a number of different execution paths),
but it requires a selector that evaluates to an integral value at compile-time.
If you want to use, for example, a <B>string</B> object as a selector, it
won’t work in a <B>switch</B> statement. For a <B>string</B> selector, you
must instead use a series of <B>if</B> statements and compare the <B>string</B>
inside the conditional.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The menu example shown above provides a
particularly nice example of a <B>switch</B>:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Menu2.cpp</font>
<font color=#009900>// A menu using a switch statement</font>
#include <iostream>
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#0000ff>int</font> main() {
<font color=#0000ff>bool</font> quit = <font color=#0000ff>false</font>; <font color=#009900>// Flag for quitting</font>
<font color=#0000ff>while</font>(quit == <font color=#0000ff>false</font>) {
cout << <font color=#004488>"Select a, b, c or q to quit: "</font>;
<font color=#0000ff>char</font> response;
cin >> response;
<font color=#0000ff>switch</font>(response) {
<font color=#0000ff>case</font> 'a' : cout << <font color=#004488>"you chose 'a'"</font> << endl;
<font color=#0000ff>break</font>;
<font color=#0000ff>case</font> 'b' : cout << <font color=#004488>"you chose 'b'"</font> << endl;
<font color=#0000ff>break</font>;
<font color=#0000ff>case</font> 'c' : cout << <font color=#004488>"you chose 'c'"</font> << endl;
<font color=#0000ff>break</font>;
<font color=#0000ff>case</font> 'q' : cout << <font color=#004488>"quitting menu"</font> << endl;
quit = <font color=#0000ff>true</font>;
<font color=#0000ff>break</font>;
<font color=#0000ff>default</font> : cout << <font color=#004488>"Please use a,b,c or q!"</font>
<< endl;
}
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>quit </B>flag is a
<A NAME="Index488"></A><A NAME="Index489"></A><B>bool</B>, short for
“Boolean,” which is a type you’ll find only in C++. It can
have only the keyword values <B>true</B> or <B>false</B>.<B> </B>Selecting
‘q’ sets the <B>quit</B> flag to <B>true</B>. The next time the
selector is evaluated, <B>quit == false</B> returns <B>false</B> so the body of
the <B>while</B> does not
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -