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 5300
.
Or you explicitly declare the variable, in which case it wonโt have a value initially:
๐๐ money ๐ก
๐๐
means declaringmoney
is the name of the variable๐ก
is the type of the variable
Instructions
Declare a mutable variable named quarters
and assign it a value of 8
.
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.