1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 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>Chapter
7: More Operators
</title>
10 <link href=
"sx6d.html" rev=precedes
>
11 <link href=
"sx7a.html" rel=precedes
>
12 <link href=
"top.html" rev=subdocument
>
15 <H1>Chapter
7: More Operators
</H1>
17 <p>In this chapter we'll meet some
18 (though still not all)
19 of C's more advanced arithmetic operators.
20 The ones we'll meet here have to do with making common patterns of
22 </p><p>It's extremely common in programming to have to
23 <dfn>increment
</dfn> a variable by
1, that is, to add
1 to it.
24 (For example, if you're processing each element of an array,
25 you'll typically write a loop with an index or pointer variable
26 stepping through the elements of the array,
27 and you'll increment the variable each time through the loop.)
28 The classic way to increment a variable is with an assignment like
32 Such an assignment is perfectly common and acceptable, but it
33 has a few slight problems:
34 <OL><li>As we've mentioned, it looks a little odd,
35 especially from an algebraic perspective.
36 <li>If the object being incremented is
37 not a simple variable,
38 the idiom can become cumbersome to type,
39 and correspondingly more error-prone.
40 For example, the expression
42 a[i+j+
2*k] = a[i+j+
2*k] +
1
45 and you may have to look closely to see that the
49 a[i+j+
2*k] = a[i+j+
2+k] +
1
53 <li>Since incrementing
56 is
<em>so
</em> common,
57 it might be nice to have an easier way of doing it.
59 C provides not one but two other,
60 simpler ways of incrementing
66 </p><p><a href=
"sx7a.html" rel=subdocument
>7.1 Assignment Operators
</a></p>
67 <p><a href=
"sx7b.html" rel=subdocument
>7.2 Increment and Decrement Operators
</a></p>
68 <p><a href=
"sx7c.html" rel=subdocument
>7.3 Order of Evaluation
</a></p>
72 <a href=
"sx6d.html" rev=precedes
>prev
</a>
73 <a href=
"sx7a.html" rel=precedes
>next
</a>
74 <a href=
"top.html" rev=subdocument
>up
</a>
75 <a href=
"top.html">top
</a>
78 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
79 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
80 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>