“In the military, I learned that ‘leadership’ means raising your hand and volunteering for the tough important assignments” Tulsi Gabbard
In this section we’re assuming you’re familiar with operators in general and variables.
In order to assign a value to the memory space of a variable, we use assignment operators.
Example:
int iVar = 10;
The = symbol is used to assign the value 10 directly to the memory-space that iVar is pointing at.
In the table below you can see the rest of assignment operators:
| Operator | Symbol |
|---|---|
| Assignment operator | = |
| Addition and Assignment operator | += |
| Subtract and Assignment operator | -= |
| Multiplication and Assignment operator | *= |
| Division and Assignment operator | /= |
Now let’s get into the details of each of these operators:
As mentioned at the beginning of this section, via this operator we can assign a value to a variable. You should know that this operator will replace the value that is already stored in the target variable (if any).
Example:
double dVar = 2.033;
In this example the value 2.033 is assigned to the variable named dVar.
Addition and Assignment `+=` Operator:
This is a compound operator which is basically the combination of addition + and assignment = operator.
What it does is that, it will select the value that is already stored in the memory space of the target variable and add it to the new value and after that, stores the result in the memory space of the variable.
Example:
int vary = 100;
vary += 200;
The value of the vary at first assignment was 100. We then used the += assignment which caused the value inside the vary to be added to the value 200 and so now the final value stored in the vary variable is 300.
Note: the example above could be rewritten like this:
int vary = 100;
vary = vary + 200;
Subtract and Assignment `-=` Operator:
This operator is a quick way of subtracting a value of a variable from another value and assigning the result to the variable itself.
Example:
int vary = 50;
vary -= 30;
The example above could be rewritten like this:
int vary = 50;
vary = vary - 30;
The final value stored in the vary variable for both the examples above is 20.
Multiplication and Assignment `*=` Operator:
If we want to quickly multiply the value of a variable to another value and assign the result to the variable itself, the *= operator is the perfect candidate.
Let’s take a look at an example:
int multiply = 10;
multiply *= 20;
The final value that will be stored in the multiply variable is 200.
What happened is that via *= operator, we multiplied the value stored in the multiply variable which is 10 to the number 20 and assigned the result to the multiply variable.
The example above could be rewritten like this:
int multiply = 10;
multiply = multiply *20;
The result of this example is also 200.
Division and Assignment `/=` Operator:
If we want to quickly divide the value of a variable by another value and assign the result to the variable itself, the /= operator is the perfect candidate.
Let’s take a look at an example:
int division = 100;
division /= 20;
The final value that will be stored in the division variable is 5. What happened is that via /= operator, we divided the value stored in the division variable which is 100 by the number 20 and assigned the result to the division variable.
The example above could be rewritten like this:
int division = 100;
division = division /20;
The result of this example is also 5.