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>3.4 <TT>while
</TT> Loops
</title>
10 <link href=
"sx3c.html" rev=precedes
>
11 <link href=
"sx3e.html" rel=precedes
>
12 <link href=
"sx3.html" rev=subdocument
>
15 <H2>3.4 <TT>while
</TT> Loops
</H2>
17 <p>[This section corresponds to half of K
&R Sec.
3.5]
18 </p><p>Loops generally consist of two parts:
19 one or more
<dfn>control expressions
</dfn> which
21 control the execution of the loop,
22 and the
<dfn>body
</dfn>,
23 which is the statement or set of statements
24 which is executed over and over.
25 </p><p>The most basic
<dfn>loop
</dfn> in C is the
28 has one control expression,
29 and executes as long as
32 This example repeatedly doubles the number
2
34 and prints the resulting numbers as long as they are less than
1000:
45 we've used braces
<TT>{}
</TT> to enclose the group of statements
46 which are to be executed together as the body of the loop.)
47 </p><p>The general syntax of a
<TT>while
</TT> loop is
49 while(
<I>expression
</I> )
52 A
<TT>while
</TT> loop starts out like an
<TT>if
</TT> statement:
53 if the condition expressed by the
<I>expression
</I> is true,
54 the
<I>statement
</I> is executed.
56 after executing the statement,
57 the condition is tested again,
58 and if it's still true,
59 the statement is executed again.
60 (Presumably, the condition depends on some value which is
61 changed in the body of the loop.)
62 As long as the condition remains true,
63 the body of the loop is executed over and over again.
64 (If the condition is false right at the start,
65 the body of the loop is not executed at all.)
67 example, if you wanted to print a number of blank lines,
68 with the variable
<TT>n
</TT> holding the number of blank
69 lines to be printed, you might use code like this:
77 After the loop finishes
78 (when control ``falls out'' of it,
79 due to the condition being false),
80 <TT>n
</TT> will have the value
0.
82 </p><p>You use a
<TT>while
</TT> loop when
83 you have a statement or group of statements which may have to
84 be executed a number of times to complete their task.
85 The controlling expression represents the condition ``the
86 loop is not done'' or ``there's more work to do.''
87 As long as the expression is true, the body of the loop is executed;
88 presumably, it makes at least some progress at its task.
89 When the expression becomes false, the task is done, and the
90 rest of the program (beyond the loop) can proceed.
91 When we think about a loop in this way,
92 we can seen an additional important property:
93 if the expression evaluates to ``false''
94 before the very first trip through the loop,
95 we make
<em>zero
</em> trips through the loop.
97 if the task is already done
98 (if there's no work to do)
99 the body of the loop is not executed at all.
100 (It's always a good idea
101 to think about the ``boundary conditions'' in a piece of code,
102 and to make sure that the code will work correctly
103 when there is no work to do,
104 or when there is a trivial task to do,
105 such as sorting an array of one number.
106 Experience has shown that bugs at boundary conditions
112 <a href=
"sx3c.html" rev=precedes
>prev
</a>
113 <a href=
"sx3e.html" rel=precedes
>next
</a>
114 <a href=
"sx3.html" rev=subdocument
>up
</a>
115 <a href=
"top.html">top
</a>
118 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
119 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
120 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>