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
2.10: Assignment Operators and Expressions
</title>
10 <link href=
"sx5i.html" rev=precedes
>
11 <link href=
"sx5k.html" rel=precedes
>
12 <link href=
"sx5.html" rev=subdocument
>
15 <H2>section
2.10: Assignment Operators and Expressions
</H2>
18 <p>You may wonder what it means to say that
19 ``
<I>expr
</I><tt><sub
></tt>1<tt></sub
></tt> is computed only once''
20 since in an assignment like
22 </pre>we don't ``evaluate''
23 the
<TT>i
</TT> on the left hand side of the
<TT>=
</TT> at all,
25 The distinction becomes important,
28 (
<I>expr
</I><tt><sub
></tt>1<tt></sub
></tt>)
29 is more complicated than a simple variable.
33 the
<TT>n
</TT> cells of an array
<TT>a
</TT>
38 </pre>If we tried to use the expanded form,
43 </pre>and by trying to increment
<TT>i
</TT> twice within the same expression
44 we'd get (as we'll see) undesired, unpredictable,
45 and in fact undefined results.
47 a more natural form of this loop would be
48 <pre> for(i =
0; i
< n; i++)
50 </pre>and with the increment of
<TT>i
</TT> moved out of the array subscript,
51 it wouldn't matter so much whether we used
<TT>a[i] +=
2</TT>
52 or
<TT>a[i] = a[i] +
2</TT>.)
54 </p><p>To make the point more clear,
56 the ``complicated expression'' without using
<TT>+=
</TT> would look like
57 <pre> yyval[yypv[p3+p4] + yypv[p1+p2]] = yyval[yypv[p3+p4] + yypv[p1+p2]] +
2
58 </pre>(What's going on here is that the subexpression
59 <TT>yypv[p3+p4] + yypv[p1+p2]
</TT> is being used as a subscript
60 to determine which cell of the
<TT>yyval
</TT> array to increment by
2.)
64 that includes the words
65 ``the assignment statement has a value''
67 an assignment is really an expression in C.
70 and it can therefore participate as a subexpression
71 in a larger expression.
72 (If the distinction between the terms ``statement'' and
76 we'll start talking about statements in the next chapter.)
80 <a href=
"sx5i.html" rev=precedes
>prev
</a>
81 <a href=
"sx5k.html" rel=precedes
>next
</a>
82 <a href=
"sx5.html" rev=subdocument
>up
</a>
83 <a href=
"top.html">top
</a>
86 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
87 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
88 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>