Sometimes in our programs, however, we need variables whose values can be modified.
If you try to reassign a constant variable, this will result in a compiler error. This is where mutable variables come in.
Mutable variables are variables whose values can be modified.
Before you can use a mutable variable you need to declare it. There are two ways, either you declare and assign the variable to a value in one step:
5300 ➡️ 🖍🆕 money
Here, we are declaring a mutable variable named
money and assigning it a value of
Or you explicitly declare the variable, in which case it won’t have a value initially:
🖍🆕 money 🔡
moneyis the name of the variable
🔡is the type of the variable
Declare a mutable variable named
quarters and assign it a value of
The compiler will issue a warning when the code is run. This is because it expects us to change the value of the mutable variable. We will learn a way to do so in the next exercise.