Arithmetic Expressions and the Assignment Operator
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
scorestarts off with the value of100. - The expression
score + 25is evaluated to125. - This value is assigned to
scoreand nowscorehas the value125.
As program code gets increasingly more complicated, drawing a diagram to trace the value of the variable can be really helpful.