* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx3d.html
blob3adbbcc5acd81272deea219b7adbab9f2560b326
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
15 <H2>3.4 <TT>while</TT> Loops</H2>
17 <p>[This section corresponds to half of K&amp;R Sec. 3.5]
18 </p><p>Loops generally consist of two parts:
19 one or more <dfn>control expressions</dfn> which
20 (not surprisingly)
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
26 <TT>while</TT> loop.
27 A <TT>while</TT> loop
28 has one control expression,
29 and executes as long as
30 that expression
31 is true.
32 This example repeatedly doubles the number 2
33 (2, 4, 8, 16, ...)
34 and prints the resulting numbers as long as they are less than 1000:
35 <pre>
36 int x = 2;
38 while(x &lt; 1000)
40 printf("%d\n", x);
41 x = x * 2;
43 </pre>
44 (Once again,
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
48 <pre>
49 while( <I>expression</I> )
50 <I>statement</I>
51 </pre>
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.
55 However,
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.)
66 </p><p>As another
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:
70 <pre>
71 while(n &gt; 0)
73 printf("\n");
74 n = n - 1;
76 </pre>
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.
96 In other words,
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
107 are quite common.)
109 </p><hr>
111 Read sequentially:
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>
116 </p>
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>
121 </p>
122 </body>
123 </html>