* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx7.html
blob7fee118a1f74c53af17bb4f8086d8f889f0b1ebc
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
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
21 operations easier.
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
29 <pre>
30 i = i + 1
31 </pre>
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
41 <pre>
42 a[i+j+2*k] = a[i+j+2*k] + 1
43 </pre>
44 is a bit of a mess,
45 and you may have to look closely to see that the
46 similar-looking
47 expression
48 <pre>
49 a[i+j+2*k] = a[i+j+2+k] + 1
50 </pre>
51 probably has
52 a mistake in it.
53 <li>Since incrementing
54 things
56 is <em>so</em> common,
57 it might be nice to have an easier way of doing it.
58 </OL>In fact,
59 C provides not one but two other,
60 simpler ways of incrementing
61 variables
62 and
63 performing other
64 similar
65 operations.
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>
69 <hr>
70 <p>
71 Read sequentially:
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>
76 </p>
77 <p>
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>
81 </p>
82 </body>
83 </html>