📄 chapter3.html
字号:
The <tt>for</tt> is preferable when there is a simple initialization andincrement since it keeps the loop control statements close together andvisible at the top of the loop. This is most obvious in<pre> for (i = 0; i < n; i++) ...</pre>which is the C idiom for processing the first <tt>n</tt> elements of an array,the analog of the Fortran DO loop or the Pascal <tt>for</tt>. The analogy is notperfect, however, since the index variable <tt>i</tt> retains its value when theloop terminates for any reason. Because the components of the <tt>for</tt> arearbitrary expressions, <tt>for</tt> loops are not restricted to arithmeticprogressions. Nonetheless, it is bad style to force unrelated computationsinto the initialization and increment of a <tt>for</tt>, which are betterreserved for loop control operations.<p>As a larger example, here is another version of <tt>atoi</tt> for converting astring to its numeric equivalent. This one is slightly more general than theone in <a href="chapter3.html">Chapter 2</a>; it copes with optional leading white spaceand an optional <tt>+</tt> or <tt>-</tt> sign. (<a href="chapter4.html">Chapter 4</a>shows <tt>atof</tt>, which does the same conversion for floating-point numbers.)<p>The structure of the program reflects the form of the input:<p><em> skip white space, if any<br> get sign, if any<br> get integer part and convert it</em><p>Each step does its part, and leaves things in a clean state for the next. Thewhole process terminates on the first character that could not be part of anumber.<pre> #include <ctype.h> /* atoi: convert s to integer; version 2 */ int atoi(char s[]) { int i, n, sign; for (i = 0; isspace(s[i]); i++) /* skip white space */ ; sign = (s[i] == '-') ? -1 : 1; if (s[i] == '+' || s[i] == '-') /* skip sign */ i++; for (n = 0; isdigit(s[i]); i++) n = 10 * n + (s[i] - '0'); return sign * n; }</pre>The standard library provides a more elaborate function <tt>strtol</tt>for conversion of strings to long integers; see<a href="appb.html#sb.5">Section 5 of Appendix B</a>.<p>The advantages of keeping loop control centralized are even more obvious whenthere are several nested loops. The following function is a Shell sort forsorting an array of integers. The basic idea of this sorting algorithm, whichwas invented in 1959 by D. L. Shell, is that in early stages, far-apartelements are compared, rather than adjacent ones as in simpler interchangesorts. This tends to eliminate large amounts of disorder quickly, so laterstages have less work to do. The interval between compared elements isgradually decreased to one, at which point the sort effectively becomes anadjacent interchange method.<pre> /* shellsort: sort v[0]...v[n-1] into increasing order */ void shellsort(int v[], int n) { int gap, i, j, temp; for (gap = n/2; gap > 0; gap /= 2) for (i = gap; i < n; i++) for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) { temp = v[j]; v[j] = v[j+gap]; v[j+gap] = temp; } }</pre>There are three nested loops. The outermost controls the gap betweencompared elements, shrinking it from <tt>n/2</tt> by a factor of two eachpass until it becomes zero. The middle loop steps along the elements. Theinnermost loop compares each pair of elements that is separated by <tt>gap</tt>and reverses any that are out of order. Since <tt>gap</tt> is eventually reducedto one, all elements are eventually ordered correctly. Notice how thegenerality of the <tt>for</tt> makes the outer loop fit in the same form as theothers, even though it is not an arithmetic progression.<p>One final C operator is the comma ``<tt>,</tt>'', which most often finds use inthe <tt>for</tt> statement. A pair of expressions separated by a comma isevaluated left to right, and the type and value of the result are the typeand value of the right operand. Thus in a for statement, it is possible toplace multiple expressions in the various parts, for example to process twoindices in parallel. This is illustrated in the function <tt>reverse(s)</tt>,which reverses the string <tt>s</tt> in place.<pre> #include <string.h> /* reverse: reverse string s in place */ void reverse(char s[]) { int c, i, j; for (i = 0, j = strlen(s)-1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } }</pre>The commas that separate function arguments, variables in declarations, etc.,are <em>not</em> comma operators, and do not guarantee left to right evaluation.<p>Comma operators should be used sparingly. The most suitable uses are forconstructs strongly related to each other, as in the <tt>for</tt> loop in<tt>reverse</tt>, and in macros where a multistep computation has to be a singleexpression. A comma expression might also be appropriate for the exchange ofelements in <tt>reverse</tt>, where the exchange can be thought of a singleoperation:<pre> for (i = 0, j = strlen(s)-1; i < j; i++, j--) c = s[i], s[i] = s[j], s[j] = c;</pre><strong>Exercise 3-3.</strong> Write a function <tt>expand(s1,s2)</tt> that expandsshorthand notations like <tt>a-z</tt> in the string <tt>s1</tt> into the equivalentcomplete list <tt>abc...xyz</tt> in <tt>s2</tt>. Allow for letters of either caseand digits, and be prepared to handle cases like <tt>a-b-c</tt> and <tt>a-z0-9</tt>and <tt>-a-z</tt>. Arrange that a leading or trailing <tt>-</tt> is takenliterally.<h2><a name="s3.6">3.6 Loops - Do-While</a></h2>As we discussed in <a href="chapter1.html">Chapter 1</a>, the <tt>while</tt>and <tt>for</tt> loops test the termination condition at the top. By contrast,the third loop in C, the <tt>do-while</tt>, tests at the bottom <em>after</em>making each pass through the loop body; the body is always executed at leastonce.<p>The syntax of the <tt>do</tt> is<pre> do <em>statement</em> while (<em>expression</em>);</pre>The <em>statement</em> is executed, then <em>expression</em> is evaluated. Ifit is true, <em>statement</em> is evaluated again, and so on. When the expressionbecomes false, the loop terminates. Except for the sense of the test,<tt>do-while</tt> is equivalent to the Pascal <tt>repeat-until</tt> statement.<p>Experience shows that <tt>do-while</tt> is much less used than <tt>while</tt> and<tt>for</tt>. Nonetheless, from time to time it is valuable, as in the followingfunction <tt>itoa</tt>, which converts a number to a character string (the inverseof <tt>atoi</tt>). The job is slightly more complicated than might be thought atfirst, because the easy methods of generating the digits generate them in thewrong order. We have chosen to generate the string backwards, then reverse it.<pre> /* itoa: convert n to characters in s */ void itoa(int n, char s[]) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n positive */ i = 0; do { /* generate digits in reverse order */ s[i++] = n % 10 + '0'; /* get next digit */ } while ((n /= 10) > 0); /* delete it */ if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse(s); }</pre>The <tt>do-while</tt> is necessary, or at least convenient, since at least onecharacter must be installed in the array <tt>s</tt>, even if <tt>n</tt> is zero.We also used braces around the single statement that makes up the body of the<tt>do-while</tt>, even though they are unnecessary, so the hasty reader will notmistake the <tt>while</tt> part for the <em>beginning</em> of a <tt>while</tt> loop.<p><strong>Exercise 3-4.</strong> In a two's complement number representation, our versionof <tt>itoa</tt> does not handle the largest negative number, that is, the valueof <tt>n</tt> equal to -(2<sup>wordsize-1</sup>). Explain why not. Modify it toprint that value correctly, regardless of the machine on which it runs.<p><strong>Exercise 3-5.</strong> Write the function <tt>itob(n,s,b)</tt> that converts theinteger <tt>n</tt> into a base <tt>b</tt> character representation in the string<tt>s</tt>. In particular, <tt>itob(n,s,16)</tt> formats <tt>s</tt> as a hexadecimalinteger in <tt>s</tt>.<p><strong>Exercise 3-6.</strong> Write a version of <tt>itoa</tt> that acceptsthree arguments instead of two. The third argument is a minimum field width;the converted number must be padded with blanks on the left if necessary tomake it wide enough.<h2><a name="s3.7">3.7 Break and Continue</a></h2>It is sometimes convenient to be able to exit from a loop other than bytesting at the top or bottom. The <tt>break</tt> statement provides an earlyexit from <tt>for</tt>, <tt>while</tt>, and <tt>do</tt>, just as from <tt>switch</tt>.A <tt>break</tt> causes the innermost enclosing loop or <tt>switch</tt> to beexited immediately.<p>The following function, <tt>trim</tt>, removes trailing blanks, tabs andnewlines from the end of a string, using a <tt>break</tt> to exit from a loopwhen the rightmost non-blank, non-tab, non-newline is found.<pre> /* trim: remove trailing blanks, tabs, newlines */ int trim(char s[]) { int n; for (n = strlen(s)-1; n >= 0; n--) if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') break; s[n+1] = '\0'; return n; }</pre><tt>strlen</tt> returns the length of the string. The <tt>for</tt> loop starts at theend and scans backwards looking for the first character that is not a blank ortab or newline. The loop is broken when one is found, or when <tt>n</tt> becomesnegative (that is, when the entire string has been scanned). You should verifythat this is correct behavior even when the string is empty or contains onlywhite space characters.<p>The <tt>continue</tt> statement is related to <tt>break</tt>, but less often used;it causes the next iteration of the enclosing <tt>for</tt>, <tt>while</tt>, or<tt>do</tt> loop to begin. In the <tt>while</tt> and <tt>do</tt>, this means that the testpart is executed immediately; in the <tt>for</tt>, control passes to the incrementstep. The <tt>continue</tt> statement applies only to loops, not to <tt>switch</tt>.A <tt>continue</tt> inside a <tt>switch</tt> inside a loop causes the next loopiteration.<p>As an example, this fragment processes only the non-negative elements in thearray <tt>a</tt>; negative values are skipped.<pre> for (i = 0; i < n; i++) if (a[i] < 0) /* skip negative elements */ continue; ... /* do positive elements */</pre>The <tt>continue</tt> statement is often used when the part of the loop thatfollows is complicated, so that reversing a test and indenting another levelwould nest the program too deeply.<h2><a name="s3.8">3.8 Goto and labels</a></h2>C provides the infinitely-abusable <tt>goto</tt> statement, and labels to branchto. Formally, the <tt>goto</tt> statement is never necessary, and in practice itis almost always easy to write code without it. We have not used <tt>goto</tt>in this book.<p>Nevertheless, there are a few situations where <tt>goto</tt>s may find a place.The most common is to abandon processing in some deeply nested structure, suchas breaking out of two or more loops at once. The <tt>break</tt> statement cannotbe used directly since it only exits from the innermost loop. Thus:<pre> for ( ... ) for ( ... ) { ... if (disaster) goto error; } ... error: /* clean up the mess */</pre>This organization is handy if the error-handling code is non-trivial, and iferrors can occur in several places.<p>A label has the same form as a variable name, and is followed by a colon. Itcan be attached to any statement in the same function as the <tt>goto</tt>.The scope of a label is the entire function.<p>As another example, consider the problem of determining whether two arrays<tt>a</tt> and <tt>b</tt> have an element in common. One possibility is<pre> for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; /* didn't find any common element */ ... found: /* got one: a[i] == b[j] */ ...</pre>Code involving a <tt>goto</tt> can always be written without one, though perhapsat the price of some repeated tests or an extra variable. For example, thearray search becomes<pre> found = 0; for (i = 0; i < n && !found; i++) for (j = 0; j < m && !found; j++) if (a[i] == b[j]) found = 1; if (found) /* got one: a[i-1] == b[j-1] */ ... else /* didn't find any common element */ ...</pre>With a few exceptions like those cited here, code that relies on <tt>goto</tt>statements is generally harder to understand and to maintain than code without<tt>goto</tt>s. Although we are not dogmatic about the matter, it does seem that<tt>goto</tt> statements should be used rarely, if at all.<p><hr><p align="center"><a href="chapter2.html">Back to Chapter 2</a> -- <a href="kandr.html">Index</a> -- <a href="chapter4.html">Chapter 4</a><p><hr></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -