## 3.6 Loops - Do-While do-while循环

As we discussed in Chapter 1, the while and for loops test the termination condition at the top. By contrast, the third loop in C, the do-while, tests at the bottom after making each pass through the loop body; the body is always executed at least once.

The syntax of the do is

```   do
statement
while (expression);```

The statement is executed, then expression is evaluated. If it is true, statement is evaluated again, and so on. When the expression becomes false, the loop terminates. Except for the sense of the test, do-while is equivalent to the Pascal repeat-until statement.

do-while循环的语法形式如下

```   do
statement
while (expression);```

Experience shows that do-while is much less used than while and for. Nonetheless, from time to time it is valuable, as in the following function itoa, which converts a number to a character string (the inverse of atoi). The job is slightly more complicated than might be thought at first, because the easy methods of generating the digits generate them in the wrong order. We have chosen to generate the string backwards, then reverse it.

```   1    /* itoa:  convert n to characters in s */
2    void itoa(int n, char s[])
3    {
4        int i, sign;
5
6        if ((sign = n) < 0)  /* record sign */
7            n = -n;          /* make n positive */
8        i = 0;
9        do {      /* generate digits in reverse order */
10            s[i++] = n % 10 + '0';  /* get next digit */
11        } while ((n /= 10) > 0);    /* delete it */
12        if (sign < 0)
13            s[i++] = '-';
14        s[i] = '\0';
15        reverse(s);
16    }
```

```   1    /* itoa:  convert n to characters in s */
2    void itoa(int n, char s[])
3    {
4        int i, sign;
5
6        if ((sign = n) < 0)  /* record sign */
7            n = -n;          /* make n positive */
8        i = 0;
9        do {      /* generate digits in reverse order */
10            s[i++] = n % 10 + '0';  /* get next digit */
11        } while ((n /= 10) > 0);    /* delete it */
12        if (sign < 0)
13            s[i++] = '-';
14        s[i] = '\0';
15        reverse(s);
16    }
```

The do-while is necessary, or at least convenient, since at least one character must be installed in the array s, even if n is zero. We also used braces around the single statement that makes up the body of the do-while, even though they are unnecessary, so the hasty reader will not mistake the while part for the beginning of a while loop.

Exercise 3-4. In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2wordsize-1). Explain why not. Modify it to print that value correctly, regardless of the machine on which it runs.

Exercise 3-5. Write the function itob(n,s,b) that converts the integer n into a base b character representation in the string s. In particular, itob(n,s,16) formats s as a hexadecimal integer in s.

Exercise 3-6. Write a version of itoa that accepts three 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 to make it wide enough.

TCPL/3.6_Loops_-_Do-While (last edited 2008-02-23 15:34:13 by localhost)

ch3n2k.com | Copyright (c) 2004-2022 czk.