Start learning free

LESSON 12 OF 415 · VARIABLES

Variable Reassignment in Swift: Changing a var

Lesson Read the full explanation

You can always change the value stored in a variable with the assignment operator(=):

// Initializing with 5:
var myVar = 5

// Assigning the value 10:
myVar = 10

print(myVar) // Output: 10

First, this code creates a variable named myVar and initializes it with 5.

Then, the code assigns 10 to myVar. Now, when myVar appears again in the code, the program will treat it as if it is 10.


Assigning the Value of One Variable to Another:

You can also assign the value of some variable to another variable using the assignment operator:

var myVar = 5
var myNum = 10

myVar = myNum

print(myVar) // Output: 10

First, this code creates a variable named myVar and initializes it with 5 and another variable named myNum and initializes it with 10.

Then, the contents of myNum (which is 10) is assigned to the variable myVar. Now, myVar also has the value of 10.


Assignment
Follow the Coding Tutorial and let's reassign some variables.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · VARIABLES

    Variable Reassignment in Swift, explained

    Assigning a new value

    A variable declared with var can be given a new value at any time, using the assignment operator =. The new value replaces the old one:

    var myVar = 5
    myVar = 10
    print(myVar)
    
    10
    

    The first line declares myVar and initialises it with 5. The second line has no var, so it declares nothing. It stores 10 in the variable that already exists. From then on, every use of myVar sees 10.

    Copying one variable into another

    The value on the right of = can come from another variable:

    var myVar = 5
    var myNum = 10
    myVar = myNum
    myNum = 20
    print(myVar, myNum)
    
    10 20
    

    The assignment copies the current value of myNum, which is 10. Afterwards the two variables are independent: setting myNum to 20 leaves myVar at 10.

    The new value can also be computed from the old one. The right side is evaluated first, then stored:

    var age = 24
    age = age + 10
    age += 1
    print(age)
    
    35
    

    Reassignment order: swapping two values

    Because an assignment overwrites the old value, order matters. Swapping two variables needs a third place to hold one value while the other is copied:

    var a = 1
    var b = 2
    let temp = a
    a = b
    b = temp
    print(a, b)
    
    2 1
    

    Without temp, the line a = b would destroy the original a, and both variables would end up as 2. Swift also has a shortcut, the tuple assignment (a, b) = (b, a).

    The exercise, step by step

    The starter code prints a short profile, then reaches a marked area where your code goes, then prints it again ten years later:

    var role = "Junior Developer"
    var age = 24
    var language = "Java"
    

    The second set of print lines must show a person who is ten years older, is now a senior developer, and still codes in Java.

    1. Reassign role to the string "Senior Developer". There is no var in front, because the variable already exists.
    2. Reassign age. The age is 24 and ten years pass, so the new value is 34.
    3. Do nothing to language. He never changed it, so the old value is already right.

    Only the marked area changes, and the print calls stay as they are. The second group of prints reads the variables after your edits.

    The solution in Swift

    var role = "Junior Developer"
    var age = 24
    var language = "Java"
    
    // Today
    print("His age is \(age)")
    print("He is a \(role)")
    print("He codes in \(language)")
    
    // ---------------------------------------
    // Write your code here:
    
    role = "Senior Developer"
    age = 34
    
    // ---------------------------------------
    
    // Ten years later:
    print("Ten years later, his age is \(age)")
    print("He is a \(role)")
    print("He still codes in \(language)")
    

    The first three lines of output still show 24, Junior Developer and Java, because they ran before the reassignment. The last three show 34, Senior Developer and Java.

    Swift details

    • Do not repeat var. Writing var role = "Senior Developer" a second time in the same scope is an error: invalid redeclaration of 'role'.
    • The type stays the same. age was inferred as an Int, so age = "old" does not compile: cannot assign value of type 'String' to type 'Int'.
    • Only var can be reassigned. A name declared with let is a constant, and assigning to it is a compile error.
    • Order matters. Code runs from top to bottom, so a line that reads a variable sees whatever value the most recent assignment left there.

    Common mistakes

    • Declaring again instead of assigning, with var in front of the new value.
    • Forgetting to update a related value. Changing role but not age leaves the story half updated.
    • Changing language as well. The task says it stays the same.
    • Assigning a value of another type, such as quotes around 34.

    Key takeaways

    • name = value replaces what a var holds.
    • Assignment copies a value, and later changes to the source do not affect the copy.
    • A reassignment has no var and must use the variable's existing type.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free