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.5 <TT>for
</TT> Loops
</title>
10 <link href=
"sx3d.html" rev=precedes
>
11 <link href=
"sx3f.html" rel=precedes
>
12 <link href=
"sx3.html" rev=subdocument
>
15 <H2>3.5 <TT>for
</TT> Loops
</H2>
17 <p>[This section corresponds to the other half of K
&R Sec.
3.5]
18 </p><p>Our second loop,
21 at least one example of already,
22 is the
<TT>for
</TT> loop.
23 The first one we saw was:
25 for (i =
0; i
< 10; i = i +
1)
26 printf(
"i is %d\n", i);
28 More generally, the syntax of a
<TT>for
</TT> loop is
30 for(
<I>expr
<tt><sub
></tt>1<tt></sub
></tt></I> ;
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I> ;
<I>expr
<tt><sub
></tt>3<tt></sub
></tt></I> )
33 (Here we see that the
<TT>for
</TT> loop has three control expressions.
35 the
<I>statement
</I> can be a brace-enclosed block.)
36 </p><p>Many loops are set up to cause some variable to step
37 through a range of values,
38 or, more generally, to set up an initial condition
39 and then modify some value to perform each succeeding loop
40 as long as some condition is true.
41 The three expressions in a
<TT>for
</TT> loop
42 encapsulate these conditions:
43 <I>expr
<tt><sub
></tt>1<tt></sub
></tt></I> sets up the initial condition,
44 <I>expr
<tt><sub
></tt>2<tt></sub
></tt></I> tests whether another trip through the loop should be taken,
45 and
<I>expr
<tt><sub
></tt>3<tt></sub
></tt></I> increments or updates
48 after each trip through the loop and prior to the next one.
52 <TT>i =
0</TT> as
<I>expr
<tt><sub
></tt>1<tt></sub
></tt></I>,
53 <TT>i
< 10</TT> as
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>,
54 <TT>i = i +
1</TT> as
<I>expr
<tt><sub
></tt>3<tt></sub
></tt></I>,
55 and the call to
<TT>printf
</TT> as
58 So the loop began by setting
<TT>i
</TT> to
0,
59 proceeded as long as
<TT>i
</TT> was less than
10,
60 printed out
<TT>i
</TT>'s value during each trip through the loop,
61 and added
1 to
<TT>i
</TT> between each trip through the loop.
63 When the compiler sees a
<TT>for
</TT> loop,
64 first,
<I>expr
<tt><sub
></tt>1<tt></sub
></tt></I> is evaluated.
65 Then,
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I> is evaluated, and if it is true,
66 the body of the loop (
<I>statement
</I>) is executed.
67 Then,
<I>expr
<tt><sub
></tt>3<tt></sub
></tt></I> is evaluated
68 to go to the next step,
69 and
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I> is evaluated again,
70 to see if there
<em>is
</em> a next step.
71 During the execution of a
<TT>for
</TT> loop, the sequence is:
73 <I>expr
<tt><sub
></tt>1<tt></sub
></tt></I>
74 <I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>
76 <I>expr
<tt><sub
></tt>3<tt></sub
></tt></I>
77 <I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>
79 <I>expr
<tt><sub
></tt>3<tt></sub
></tt></I>
81 <I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>
83 <I>expr
<tt><sub
></tt>3<tt></sub
></tt></I>
84 <I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>
86 </blockquote>The first thing executed is
<I>expr
<tt><sub
></tt>1<tt></sub
></tt></I>.
87 <I>expr
<tt><sub
></tt>3<tt></sub
></tt></I> is evaluated after
<em>every
</em> trip through the loop.
88 The last thing executed is always
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>, because
89 when
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I> evaluates false, the loop exits.
90 </p><p>All three expressions of a
<TT>for
</TT> loop are optional.
91 If you leave out
<I>expr
<tt><sub
></tt>1<tt></sub
></tt></I>, there simply is no initialization step,
92 and the variable(s) used with the loop had better have been
94 If you leave out
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>,
96 and the default for the
<TT>for
</TT> loop
97 is that another trip through the loop should be taken
99 unless you break out of it some other way, the loop runs forever).
100 If you leave out
<I>expr
<tt><sub
></tt>3<tt></sub
></tt></I>, there is no increment step.
101 </p><p>The semicolons separate the three controlling expressions of a
103 (These semicolons, by the way, have nothing to do with
104 statement terminators.)
106 If you leave out one or more of the expressions, the semicolons remain.
107 Therefore, one way of writing a deliberately infinite loop in C is
112 </p><p>It's useful to compare C's
<TT>for
</TT> loop to the
113 equivalent loops in other computer languages you might know.
116 for(i = x; i
<= y; i = i + z)
118 is roughly equivalent to:
120 for I = X to Y step Z
<I>(BASIC)
</I>
122 do
10 i=x,y,z
<I>(FORTRAN)
</I>
124 for i := x to y
<I>(Pascal)
</I>
126 In C (unlike FORTRAN), if the test condition is false before
127 the first trip through the loop, the loop won't be traversed at all.
128 In C (unlike Pascal), a loop control variable
129 (in this case,
<TT>i
</TT>)
130 is guaranteed to retain its final value after the loop completes,
131 and it is also legal to modify the control variable within the loop,
132 if you really want to.
133 (When the loop terminates due to the test condition turning false,
134 the value of the control variable after the loop will be the
135 first value for which the condition failed, not the last value
136 for which it succeeded.)
137 </p><p>It's also worth noting that a
<TT>for
</TT> loop can be used
138 in more general ways than the simple, iterative examples we've
140 The ``control variable'' of a
<TT>for
</TT> loop
141 does not have to be an integer, and it does not have to be
142 incremented by an additive increment.
143 It could be ``incremented'' by a multiplicative
144 factor (
1,
2,
4,
8, ...) if that was what you needed,
145 or it could be a floating-point variable,
146 or it could be another type of variable
148 which we haven't met yet
149 which would step, not over numeric values,
150 but over the elements of an array or other data structure.
151 Strictly speaking, a
<TT>for
</TT> loop doesn't have to have
152 a ``control variable'' at all;
153 the three expressions can be anything,
154 although the loop will make the most sense
155 if they are related and together form the expected
156 initialize, test, increment
158 </p><p>The powers-of-two example of the previous section
159 does fit this pattern,
160 so we could rewrite it like this:
164 for(x =
2; x
< 1000; x = x *
2)
167 </p><p>There is no earth-shaking or fundamental difference between the
168 <TT>while
</TT> and
<TT>for
</TT> loops.
169 In fact, given the general
172 for(
<I>expr
<tt><sub
></tt>1<tt></sub
></tt></I>;
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>;
<I>expr
<tt><sub
></tt>3<tt></sub
></tt></I>)
178 rewrite it as a
<TT>while
</TT> loop,
179 moving the initialize and increment expressions to statements
180 before and within the loop:
182 <I>expr
<tt><sub
></tt>1<tt></sub
></tt></I> ;
183 while(
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>)
186 <I>expr
<tt><sub
></tt>3<tt></sub
></tt></I> ;
197 you could rewrite it as a
<TT>for
</TT> loop:
204 between the
<TT>for
</TT> and
<TT>while
</TT> loops
205 is that although the test expression (
<I>expr
<tt><sub
></tt>2<tt></sub
></tt></I>)
206 is optional in a
<TT>for
</TT> loop,
207 it is required in a
<TT>while
</TT> loop.
208 If you leave out the controlling expression of a
<TT>while
</TT> loop,
209 the compiler will complain about a syntax error.
210 (To write a deliberately infinite
<TT>while
</TT> loop,
211 you have to supply an expression which is always nonzero.
212 The most obvious one would simply be
<TT>while(
1)
</TT>
214 </p><p>If it's possible to rewrite a
<TT>for
</TT> loop as a
215 <TT>while
</TT> loop and vice versa, why do they both exist?
216 Which one should you choose?
218 when you choose a
<TT>for
</TT> loop,
219 its three expressions
222 the same variable or data structure,
224 initialize, test, increment pattern.
225 If they don't manipulate the same variable or don't follow that pattern,
226 wedging them into a
<TT>for
</TT> loop buys nothing
227 and a
<TT>while
</TT> loop would probably be clearer.
228 (The reason that one loop or the other can be clearer is simply that,
229 when you see a
<TT>for
</TT> loop,
230 you
<em>expect
</em> to see an idiomatic
231 initialize/test/increment of a single variable,
232 and if the
<TT>for
</TT> loop you're looking at
233 doesn't end up matching that pattern,
234 you've been momentarily misled.)
238 <a href=
"sx3d.html" rev=precedes
>prev
</a>
239 <a href=
"sx3f.html" rel=precedes
>next
</a>
240 <a href=
"sx3.html" rev=subdocument
>up
</a>
241 <a href=
"top.html">top
</a>
244 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
245 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
246 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>