1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>section
3.6: Loops -- Do-while
</title>
10 <link href=
"sx6e.html" rev=precedes
>
11 <link href=
"sx6g.html" rel=precedes
>
12 <link href=
"sx6.html" rev=subdocument
>
15 <H2>section
3.6: Loops -- Do-while
</H2>
18 <p>Note the semicolon following the parenthesized expression
19 in the
<TT>do
</TT>-
<TT>while
</TT> loop;
20 it's a required part of the syntax.
21 </p><p>Make sure you understand the difference between a
22 <TT>while
</TT> loop and a
<TT>do
</TT>-
<TT>while
</TT> loop.
23 A
<TT>while
</TT> loop executes strictly according to its
24 conditional expression:
25 if the expression is never true,
26 the loop executes zero times.
27 The
<TT>do
</TT>-
<TT>while
</TT> loop,
29 makes an initial ``no peek'' foray through the loop body no
31 </p><p>To see the difference,
32 let's imagine three different ways of writing the loop in the
33 <TT>itoa
</TT> function on page
64.
34 Suppose we somehow forgot to use a termination condition at all,
35 and wrote something like
37 s[i++] = n %
10 + '
0';
40 </pre>Eventually,
<TT>n
</TT> becomes zero, but we keep going around the loop,
41 and we convert a number like
123 into a string like
42 <TT>"0000000000123"</TT>, except with an infinite number of
44 (Mathematically, this is correct, but it's not what we want here,
45 especially if we want our program to use a finite amount of
47 </p><p>Our next attempt might be
48 <pre> while(n
> 0) {
49 s[i++] = n %
10 + '
0';
52 </pre>so that we stop creating digits when
<TT>n
</TT> reaches
0.
53 This works fine for positive numbers,
54 but for
0, it stops too soon:
55 it would convert the number
0 to the empty string
<TT>""</TT>.
56 That's why the
<TT>do
</TT>-
<TT>while
</TT> loop is appropriate here;
57 the fact that it always makes at least one pass through the loop
58 makes sure that we always generate at least one digit,
60 </p><p>(It's also useful to look at the invariants in this loop:
61 during each trip through the loop,
62 <TT>n
</TT> contains the rest of the number we have to convert,
63 <TT>s[]
</TT> contains the digits we've already converted,
64 and
<TT>i
</TT> points at the next cell in
<TT>s[]
</TT> which is to receive a digit.
65 Each trip through the loop converts one digit,
66 increments
<TT>i
</TT>,
67 and divides
<TT>n
</TT> by
10.)
71 <a href=
"sx6e.html" rev=precedes
>prev
</a>
72 <a href=
"sx6g.html" rel=precedes
>next
</a>
73 <a href=
"sx6.html" rev=subdocument
>up
</a>
74 <a href=
"top.html">top
</a>
77 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
78 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
79 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>