亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? ch07.htm

?? good book for learning c++ standard language
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
<PRE><FONT COLOR="#0066FF">1:     //Listing 7.13
2:     //Demonstrates null statement
3:     // as body of for loop
4:
5:     #include &lt;iostream.h&gt;
6:     int main()
7:     {
8:        for (int i = 0; i&lt;5; cout &lt;&lt; &quot;i: &quot; &lt;&lt; i++ &lt;&lt; endl)
9:        ;
10:       return 0;
<TT>11: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: i: 0
i: 1
i: 2
i: 3
i: 4
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>The <TT>for</TT> loop on
line 8 includes three statements: the initialization statement establishes the counter
<TT>i</TT> and initializes it to <TT>0</TT>. The condition statement tests for <TT>i&lt;5</TT>,
and the action statement prints the value in <TT>i</TT> and increments it.<BR>
There is nothing left to do in the body of the <TT>for</TT> loop, so the null statement
(<TT>;</TT>) is used. Note that this is not a well-designed <TT>for</TT> loop: the
action statement is doing far too much. This would be better rewritten as</P>
<PRE><FONT COLOR="#0066FF">8:         for (int i = 0; i&lt;5; i++)</FONT></PRE>
<PRE><FONT COLOR="#0066FF">
9:              cout &lt;&lt; &quot;i: &quot; &lt;&lt; i &lt;&lt; endl;</FONT></PRE>
<P><FONT COLOR="#0066FF"><BR>
</FONT>While both do exactly the same thing, this example is easier to understand.
<H4 ALIGN="CENTER"><A NAME="Heading47"></A><FONT COLOR="#000077">Nested Loops</FONT></H4>
<P>Loops may be nested, with one loop sitting in the body of another. The inner loop
will be executed in full for every execution of the outer loop. Listing 7.14 illustrates
writing marks into a matrix using nested <TT>for</TT> loops.</P>
<P><A NAME="Heading48"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 7.14. Illustrates
nested for loops.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1:   //Listing 7.14
2:   //Illustrates nested for loops
3:
4:   #include &lt;iostream.h&gt;
5:
6:   int main()
7:   {
8:       int rows, columns;
9:       char theChar;
10:       cout &lt;&lt; &quot;How many rows? &quot;;
11:       cin &gt;&gt; rows;
12:       cout &lt;&lt; &quot;How many columns? &quot;;
13:       cin &gt;&gt; columns;
14:       cout &lt;&lt; &quot;What character? &quot;;
15:       cin &gt;&gt; theChar;
16:       for (int i = 0; i&lt;rows; i++)
17:       {
18:          for (int j = 0; j&lt;columns; j++)
19:              cout &lt;&lt; theChar;
20:          cout &lt;&lt; &quot;\n&quot;;
21:       }
22:      return 0;
<TT>23: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: How many rows? 4
How many columns? 12
What character? x
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>The user is prompted for the number
of <TT>rows</TT> and <TT>columns</TT> and for a character to print. The first <TT>for</TT>
loop, on line 16, initializes a counter (<TT>i</TT>) to <TT>0</TT>, and then the
body of the outer for loop is run.<BR>
On line 18, the first line of the body of the outer <TT>for</TT> loop, another <TT>for</TT>
loop is established. A second counter (<TT>j</TT>) is also initialized to <TT>0</TT>,
and the body of the inner <TT>for</TT> loop is executed. On line 19, the chosen character
is printed, and control returns to the header of the inner <TT>for</TT> loop. Note
that the inner <TT>for</TT> loop is only one statement (the printing of the character).
The condition is tested (<TT>j &lt; columns</TT>) and if it evaluates <TT>true</TT>,
<TT>j</TT> is incremented and the next character is printed. This continues until
<TT>j</TT> equals the number of columns.</P>
<P>Once the inner <TT>for</TT> loop fails its test, in this case after 12 <TT>X</TT>s
are printed, execution falls through to line 20, and a new line is printed. The outer
<TT>for</TT> loop now returns to its header, where its condition (<TT>i &lt; rows</TT>)
is tested. If this evaluates <TT>true</TT>, <TT>i</TT> is incremented and the body
of the loop is executed.</P>
<P>In the second iteration of the outer <TT>for</TT> loop, the inner <TT>for</TT>
loop is started over. Thus, <TT>j</TT> is reinitialized to <TT>0</TT> and the entire
inner loop is run again.</P>
<P>The important idea here is that by using a nested loop, the inner loop is executed
for each iteration of the outer loop. Thus the character is printed <TT>columns</TT>
times for each row.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>As an aside, many C++ programmers
	use the letters <TT>i</TT> and <TT>j</TT> as counting variables. This tradition goes
	all the way back to FORTRAN, in which the letters <TT>i</TT>, <TT>j</TT>, <TT>k</TT>,
	<TT>l</TT>, <TT>m</TT>, and <TT>n</TT> were the only legal counting variables. Other
	programmers prefer to use more descriptive counter variable names, such as <TT>Ctrl</TT>
	and <TT>Ctr2</TT>. Using <TT>i</TT> and <TT>j</TT> in <TT>for</TT> loop headers should
	not cause much confusion, however. 
<HR>


</BLOCKQUOTE>

<H4 ALIGN="CENTER"><A NAME="Heading50"></A><FONT COLOR="#000077">Scoping in for Loops</FONT></H4>
<P>You will remember that variables are scoped to the block in which they are created.
That is, a local variable is visible only within the block in which it is created.
It is important to note that counting variables created in the header of a <TT>for</TT>
loop are scoped to the outer block, not the inner block. The implication of this
is that if you have two <TT>for</TT> loops in the same function, you must give them
different counter variables, or they may interfere with one another.
<H3 ALIGN="CENTER"><A NAME="Heading51"></A><FONT COLOR="#000077">Summing Up Loops</FONT></H3>
<P>On Day 5, &quot;Functions,&quot; you learned how to solve the Fibonacci series
problem using recursion. To review briefly, a Fibonacci series starts with 1, 1,
2, 3, and all subsequent numbers are the sum of the previous two:</P>
<PRE><FONT COLOR="#0066FF">1,1,2,3,5,8,13,21,34...
</FONT></PRE>
<P>The nth Fibonacci number is the sum of the n-1 and the n-2 Fibonacci numbers.
The problem solved on Day 5 was finding the value of the nth Fibonacci number. This
was done with recursion. Listing 7.15 offers a solution using iteration.</P>
<P><A NAME="Heading52"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 7.15. Solving
the nth Fibonacci numberusing iteration.</B></FONT><FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1:  // Listing 7.15
2:  // Demonstrates solving the nth
3:  // Fibonacci number using iteration
4:
5:  #include &lt;iostream.h&gt;
6:
7:  typedef unsigned long int ULONG;
8:
9:  ULONG fib(ULONG position);
10:  int main()
11:  {
12:      ULONG answer, position;
13:      cout &lt;&lt; &quot;Which position? &quot;;
14:      cin &gt;&gt; position;
15:      cout &lt;&lt; &quot;\n&quot;;
16:
17:      answer = fib(position);
18:      cout &lt;&lt; answer &lt;&lt; &quot; is the &quot;;
19:      cout &lt;&lt; position &lt;&lt; &quot;th Fibonacci number.\n&quot;;
20:     return 0;
21:  }
22:
23:  ULONG fib(ULONG n)
24:  {
25:      ULONG minusTwo=1, minusOne=1, answer=2;
26:
27:      if (n &lt; 3)
28:          return 1;
29:
30:      for (n -= 3; n; n--)
31:      {
32:          minusTwo = minusOne;
33:          minusOne = answer;
34:          answer = minusOne + minusTwo;
35:      }
36:
37:      return answer;
<TT>38: }</TT>
Output: Which position? 4

3 is the 4th Fibonacci number.

Which position? 5

5 is the 5th Fibonacci number.

Which position? 20

6765 is the 20th Fibonacci number.

Which position? 100

3314859971 is the 100th Fibonacci number.
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis</B></FONT><B>: </B>Listing 7.15 solves the Fibonacci
series using iteration rather than recursion. This approach is faster and uses less
memory than the recursive solution.<BR>
On line 13, the user is asked for the position to check. The function <TT>fib()</TT>
is called, which evaluates the position. If the position is less than 3, the function
returns the value <TT>1</TT>. Starting with position 3, the function iterates using
the following algorithm:<BR>
<B><BR>
1.</B> Establish the starting position: Fill variable <TT>answer</TT> with <TT>2</TT>,
<TT>minusTwo</TT> with <TT>0</TT> (<TT>answer</TT>-2), and <TT>minusOne</TT> with
<TT>1</TT> (<TT>answer</TT>-1). Decrement the position by 3, because the first two
numbers are handled by the starting position.<BR>
<BR>
<B>2.</B> For every number, count up the Fibonacci series. This is done by<BR>
<B>a.</B> Putting the value currently in <TT>minusOne</TT> into <TT>minusTwo</TT>.<BR>
<BR>
<B>b.</B> Putting the value currently in <TT>answer</TT> into <TT>minusOne</TT>.<BR>
<BR>
<B>c.</B> Adding <TT>minusOne</TT> and <TT>minusTwo</TT> and putting the sum in <TT>answer</TT>.<BR>
<BR>
<B>d.</B> Decrementing <TT>n</TT>.<BR>
<BR>
<B>3.</B> When <TT>n</TT> reaches <TT>0</TT>, return the answer.<BR>
<BR>
This is exactly how you would solve this problem with pencil and paper. If you were
asked for the fifth Fibonacci number, you would write:</P>
<PRE><FONT COLOR="#0066FF">1, 1, 2,</FONT></PRE>
<P>and think, &quot;two more to do.&quot; You would then add <TT>2+1</TT> and write
<TT>3</TT>, and think, &quot;one more to find.&quot; Finally you would write <TT>3+2</TT>
and the answer would be <TT>5</TT>. In effect, you are shifting your attention right
one number each time through, and decrementing the number remaining to be found.</P>
<P>Note the condition tested on line 30 (<TT>n</TT>). This is a C++ idiom, and is
exactly equivalent to <TT>n != 0</TT>. This <TT>for</TT> loop relies on the fact
that when <TT>n</TT> reaches <TT>0</TT> it will evaluate <TT>false</TT>, because
<TT>0</TT> is false in C++. The <TT>for</TT> loop header could have been written:</P>
<PRE><FONT COLOR="#0066FF">for (n-=3; n&gt;0; n++)</FONT></PRE>
<P>which might have been clearer. However, this idiom is so common in C++ that there
is little sense in fighting it.</P>
<P>Compile, link, and run this program, along with the recursive solution offered
on Day 5. Try finding position 25 and compare the time it takes each program. Recursion
is elegant, but because the function call brings a performance overhead, and because
it is called so many times, its performance is noticeably slower than iteration.
Microcomputers tend to be optimized for the arithmetic operations, so the iterative
solution should be blazingly fast.</P>
<P>Be careful how large a number you enter. <TT>fib</TT> grows quickly, and long
integers will overflow after a while.
<H3 ALIGN="CENTER"><A NAME="Heading54"></A><FONT COLOR="#000077">switch Statements</FONT></H3>
<P>On Day 4, you saw how to write <TT>if</TT> and <TT>if/else</TT> statements. These
can become quite confusing when nested too deeply, and C++ offers an alternative.
Unlike <TT>if</TT>, which evaluates one value, <TT>switch</TT> statements allow you
to branch on any of a number of different values. The general form of the <TT>switch</TT>
statement is:</P>
<PRE><FONT COLOR="#0066FF">switch (expression)
{
case valueOne: statement;
                   break;
case valueTwo: statement;
                   break;
....
case valueN:   statement;
                   break;
default:       statement;
}
</FONT></PRE>
<P>expression is any legal C++ expression, and the statements are any legal C++ statements
or block of statements. <TT>switch</TT> evaluates <TT>expression</TT> and compares
the result to each of the <TT>case</TT> values. Note, however, that the evaluation
is only for equality; relational operators may not be used here, nor can Boolean
operations.</P>
<P>If one of the <TT>case</TT> values matches the expression, execution jumps to
those statements and continues to the end of the <TT>switch</TT> block, unless a
<TT>break</TT> statement is encountered. If nothing matches, execution branches to
the optional <TT>default</TT> statement. If there is no default and there is no matching
value, execution falls through the <TT>switch</TT> statement and the statement ends.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>It is almost always a good idea
	to have a <TT>default</TT> <TT>case</TT> in <TT>switch</TT> statements. If you have
	no other need for the <TT>default</TT>, use it to test for the supposedly impossible
	case, and print out an error message; this can be a tremendous aid in debugging.
	
<HR>


</BLOCKQUOTE>

<P>It is important to note that if there is no <TT>break</TT> statement at the end
of a <TT>case</TT> statement, execution will fall through to the next <TT>case</TT>
statement. This is sometimes necessary, but usually is an error. If you decide to
let execution fall through, be sure to put a comment, indicating that you didn't
just forget the <TT>break</TT>.</P>
<P>Listing 7.16 illustrates use of the <TT>switch</TT> statement.</P>
<P><A NAME="Heading55"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 7.16. Demonstrating
the switch statement</B></FONT><FONT SIZE="2" COLOR="#000077"><B>.</B></FONT><FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1:  //Listing 7.16
2:  // Demonstrates switch statement
3:
4:  #include &lt;iostream.h&gt;
5:
6:  int main()
7:  {
8:    unsigned short int number;
9:    cout &lt;&lt; &quot;Enter a number between 1 and 5: &quot;;
10:    cin &gt;&gt; number;
11:    switch (number)
12:    {
13:       case 0:   cout &lt;&lt; &quot;Too small, sorry!&quot;;
14:                 break;
15:       case 5:  cout &lt;&lt; &quot;Good job!\n&quot;;  // fall through
16:       case 4:  cout &lt;&lt; &quot;Nice Pick!\n&quot;; // fall through
17:       case 3:  cout &lt;&lt; &quot;Excellent!\n&quot;; // fall through

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美日韩精品一二三区| 日韩一区在线免费观看| 久久嫩草精品久久久精品| 中文字幕日韩一区| 日本成人在线网站| 99久免费精品视频在线观看| 欧美日韩精品一区二区三区蜜桃| 国产日产精品1区| 免费观看在线综合| 色综合久久久网| 国产亚洲一本大道中文在线| 午夜精品国产更新| 91性感美女视频| 国产亚洲精品aa午夜观看| 天天综合网 天天综合色| 99精品一区二区三区| 欧美高清激情brazzers| 亚洲欧美一区二区三区孕妇| 国产在线一区观看| 91精品国产综合久久精品麻豆| 欧美经典一区二区| 国产综合久久久久久鬼色| 欧美老肥妇做.爰bbww视频| 亚洲男人的天堂av| 不卡一区二区三区四区| 久久精品免视看| 韩国一区二区在线观看| 日韩欧美电影一二三| 亚洲丰满少妇videoshd| 色哦色哦哦色天天综合| 亚洲男人电影天堂| 97精品久久久久中文字幕| 国产精品嫩草久久久久| 国产精品一二三四| 久久九九久久九九| 国产又黄又大久久| 久久久久国产一区二区三区四区| 精品写真视频在线观看| 日韩免费性生活视频播放| 免费视频最近日韩| 日韩欧美国产成人一区二区| 免费精品视频在线| 日韩精品一区二区三区视频播放 | av网站一区二区三区| 国产日韩精品一区二区浪潮av | 国产一区二区三区在线观看精品 | 欧美在线看片a免费观看| 亚洲欧美综合网| 色婷婷综合中文久久一本| 一区二区三区视频在线看| 欧美亚洲综合网| 免费在线观看一区二区三区| 日韩一级精品视频在线观看| 精品一区二区三区免费毛片爱| 欧美电影免费观看完整版| 国产一区二区福利| 日韩一区在线免费观看| 欧美日韩在线播| 久久99精品久久久久久国产越南| 久久久精品免费免费| eeuss鲁片一区二区三区| 亚洲综合免费观看高清完整版 | 激情综合五月天| 亚洲国产精品黑人久久久| 一本大道久久a久久精二百| 亚洲国产精品久久不卡毛片| 精品久久久久99| 波多野结衣精品在线| 亚洲18影院在线观看| 久久午夜羞羞影院免费观看| 成人黄页在线观看| 肉色丝袜一区二区| 国产亚洲一区字幕| 在线免费观看不卡av| 精品一区二区三区免费观看| 国产婷婷色一区二区三区四区 | 亚洲欧洲av另类| 91麻豆精品国产无毒不卡在线观看 | 视频一区中文字幕| 国产精品三级视频| 91精品午夜视频| 91在线视频免费观看| 麻豆一区二区三| 亚洲视频精选在线| 337p粉嫩大胆噜噜噜噜噜91av | 国产成a人亚洲精| 亚洲一区二区四区蜜桃| 国产午夜精品久久久久久久| 7777女厕盗摄久久久| 成人福利视频在线看| 日本午夜精品一区二区三区电影 | 亚洲精品一区在线观看| 色天使色偷偷av一区二区| 国产美女精品一区二区三区| 亚洲高清视频在线| 亚洲视频一二三| 国产亲近乱来精品视频| 欧美男男青年gay1069videost| 成人精品在线视频观看| 国内精品写真在线观看| 日韩精品欧美精品| 亚洲午夜免费福利视频| 国产精品福利一区| 国产无人区一区二区三区| 91精品国产综合久久精品性色 | 粉嫩一区二区三区性色av| 蜜臀av一区二区在线观看| 午夜精品福利一区二区三区蜜桃| 亚洲欧美一区二区三区久本道91| 国产亚洲欧美日韩在线一区| 精品国产91久久久久久久妲己 | 亚洲在线观看免费| 亚洲欧美另类图片小说| 亚洲私人影院在线观看| 一区在线中文字幕| 亚洲国产激情av| 国产精品视频麻豆| 久久精品一区蜜桃臀影院| 精品免费视频.| 精品国产一区二区三区av性色| 91精品国产综合久久小美女| 正在播放亚洲一区| 日韩欧美亚洲另类制服综合在线| 欧美精三区欧美精三区| 欧美美女一区二区三区| 91.xcao| 日韩欧美不卡一区| 久久夜色精品国产欧美乱极品| 欧美成人aa大片| 久久久国产一区二区三区四区小说| 精品国产一区二区三区忘忧草| 精品国产百合女同互慰| 国产日韩高清在线| 亚洲日穴在线视频| 亚洲电影一级片| 老司机一区二区| 国产麻豆精品95视频| 成人av网站在线观看| 91蜜桃在线观看| 欧美麻豆精品久久久久久| 欧美一区二区三区视频在线观看| 精品国产第一区二区三区观看体验| 久久精品一区二区三区av| 亚洲婷婷综合色高清在线| 亚洲电影视频在线| 国模大尺度一区二区三区| 波多野结衣的一区二区三区| 欧美三片在线视频观看| 欧美tickling网站挠脚心| 国产精品色在线| 亚洲mv大片欧洲mv大片精品| 久久99久国产精品黄毛片色诱| 懂色av中文一区二区三区| 在线亚洲免费视频| 欧美电影免费观看高清完整版在 | 日本成人在线看| 成人激情小说乱人伦| 欧美一区二区三区思思人| 国产精品久久久久影院亚瑟 | 国产午夜一区二区三区| 亚洲与欧洲av电影| 国产精品资源在线观看| 在线观看网站黄不卡| 久久综合九色综合久久久精品综合| 亚洲视频在线一区| 极品少妇xxxx精品少妇偷拍| 色婷婷久久久亚洲一区二区三区| 精品国产一区二区三区忘忧草 | 日韩三级视频在线观看| 国产精品国产自产拍高清av王其| 午夜精品在线视频一区| 成人av在线网| 欧美v国产在线一区二区三区| 亚洲精品免费在线播放| 国产一区二区三区高清播放| 欧美电影影音先锋| 亚洲人成在线播放网站岛国| 极品少妇xxxx精品少妇| 欧美乱熟臀69xxxxxx| 亚洲人快播电影网| 成人视屏免费看| 精品国产91亚洲一区二区三区婷婷| 亚洲五码中文字幕| 91麻豆文化传媒在线观看| 国产性做久久久久久| 捆绑调教一区二区三区| 欧美精品在线视频| 一区二区三区精品| 91色乱码一区二区三区| 国产精品沙发午睡系列990531| 极品少妇一区二区| 日韩一区二区三区av| 亚洲国产毛片aaaaa无费看| 99riav一区二区三区| 国产精品久久久久久久蜜臀| 欧美日韩精品一区二区在线播放| 中文字幕在线一区| 成人h动漫精品一区二| 国产网站一区二区| 国产91精品在线观看|