[This section corresponds to Sec. 2.10]
The first and more general way is that any time you have the pattern v = v op e where v is any variable (or anything like a[i]), op is any of the binary arithmetic operators we've seen so far, and e is any expression, you can replace it with the simplified v op= e For example, you can replace the expressions i = i + 1 j = j - 10 k = k * (n + 1) a[i] = a[i] / b with i += 1 j -= 10 k *= n + 1 a[i] /= b
In an example in a previous chapter, we used the assignment a[d1 + d2] = a[d1 + d2] + 1; to count the rolls of a pair of dice. Using +=, we could simplify this expression to a[d1 + d2] += 1;
As these examples show, you can use the ``op='' form with any of the arithmetic operators (and with several other operators that we haven't seen yet). The expression, e, does not have to be the constant 1; it can be any expression. You don't always need as many explicit parentheses when using the op= operators: the expression k *= n + 1 is interpreted as k = k * (n + 1)
Bhopal news
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100