- 4.1 Introduction
- 4.2 Algorithms
- 4.3 Pseudocode
- 4.4 Control Structures
- 4.5 If...Then Selection Statement
- 4.6 If...Then...Else Selection Statement
- 4.7 Nested If...Then...Else Statements
- 4.8 Repetition Statements
- 4.9 Compound Assignment Operators
- 4.10 Formulating Algorithms: Counter-Controlled Repetition
- 4.11 Formulating Algorithms: Nested Control Statements
- 4.12 Using the Debugger: Locating a Logic Error
- 4.13 Wrap-Up
- Summary
- Terminology
- Self-Review Exercises
- Answers to Self-Review Exercises
- Quick Quiz
- Exercises
- Making a Difference Exercises
4.9 Compound Assignment Operators
The compound assignment operators enable you to abbreviate assignment statements. For example, the statement
|
which mentions the variable value on both sides of the assignment, can be abbreviated with the addition assignment operator, += as
|
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 |