The assignment operator (=
) happens after the expression is evaluated.
double taxes;
double price = 3.99;
double taxRate = 0.08;
taxes = price * taxRate;
Variables can be mutable (changeable). They can be reassigned new different values. Let's consider another example where a current score for a player gets updated.
int score = 100;
score = score + 25;
- The value of
score
starts off with the value of 100
.
- The expression
score + 25
is evaluated to 125
.
- This value is assigned to
score
and now score
has the value 125
.
As program code gets increasingly more complicated, drawing a diagram to trace the value of the variable can be really helpful.
Resources
Next Learn Tutorial
Learn: Using the Compound Assignment Operator