Learn
Variables
Mutable Variables

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 declaring
  • money is the name of the variable
  • ๐Ÿ”ก is the type of the variable

Instructions

1.

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.

Folder Icon

Take this course for free

Already have an account?