Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

4.9 Compound Assignment Operators

The compound assignment operators enable you to abbreviate assignment statements. For example, the statement

value = value + 3

which mentions the variable value on both sides of the assignment, can be abbreviated with the addition assignment operator, += as

value += 3

The += operator adds the value of the right operand to the value of the left operand and stores the result in the left operand's variable. Figure 4.8 summarizes the compound assignment operators.

Fig 4.8. Compound assignment operators.

Compound assignment operator

Sample expression

Explanation

Assigns

Assume: c = 4, d = "He"

+=

c += 7

c = c + 7

11 to c

-=

c -= 3

c = c - 3

1 to c

*=

c *= 4

c = c * 4

16 to c

/=

c /= 2

c = c / 2

2 to c

\=

c \= 3

c = c \ 3

1 to c

^=

c ^= 2

c = c ^ 2

16 to c

&=

d &= "llo"

d = d & "llo"

"Hello" to d

The variable on the left side of an assignment operator must be an lvalue ("left value")—a modifiable variable or property that can appear on the left side of an assignment statement. We'll learn how to declare constants in Section 6.10—constants cannot be lvalues.

The =, +=, -=, *=, /=, \=, ^= and &= operators are always applied last in an expression. When an assignment (=) is evaluated, the expression to the right of the operator is always evaluated first, then the value is assigned to the lvalue on the left. When a compound assignment is evaluated, the appropriate operator is applied to the lvalue's original value and the value to the operator's right, then the resulting value is assigned to the lvalue on the left.

Demonstrating the ^= Compound Assignment Operator

Figure 4.9 calculates a power of 2 using the exponentiation assignment operator. In line 8, we take advantage of a Visual Basic feature that allows variable initialization to be incorporated into a declaration. In this case, we initialize variable exponent to the value of exponentTextBox's Text property. Lines 12 and 17 each raise variable result to the value of variable exponent. The results of these two calculations are identical as shown in the sample output.

Fig 4.9. Exponentiation using a compound assignment operator.

 
1   ' Fig. 4.9: PowerOf2.vb
2   ' Calculates 2 raised to the exponent entered by the user.
3   Public Class PowerOf2
4      ' calculates 2 raised to the exponent entered by the user.
5   Private Sub calculateButton_Click(ByVal sender As System.Object,
6   ByVal e As System.EventArgs) Handles calculateButton.Click
7
8         Dim exponent As Integer = exponentTextBox.Text ' get the exponent
9         Dim result As Integer ' stores the calculation result
10

11         result = 2      ' number to raise to a power
12        result ^= exponent ' same as result = result ^ exponent

13         resultLabel.Text = "result = 2" & vbCrLf &

14        "result ^= " & exponent & ": " & result & vbCrLf & vbCrLf

15

16         result = 2 ' reset result to 2 for next calculation
17        result = result ^ exponent  ' same as result ^= exponent
18         resultLabel.Text &= "result = 2" & vbCrLf &
19            "result = result ^ " & exponent & ": " & result
20      End Sub ' calculateButton_Click
21
22      ' clear results when user types in the exponentTextBox
23      Private Sub exponentTextBox_TextChanged(ByVal sender As System.Object,
24         ByVal e As System.EventArgs) Handles exponentTextBox.TextChanged
25         resultLabel.Text = String.Empty ' clears the resultLabel's text
26      End Sub ' exponentTextBox_TextChanged
27   End Class ' PowerOf2
  • + Share This
  • 🔖 Save To Your Account