Bird
Raised Fist0
Swiftprogramming~10 mins

Var for variables (mutable) in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Var for variables (mutable)
Declare var x = 5
↓
Use x in code
↓
Change x = 10
↓
Use new x value
↓
End
This flow shows how a mutable variable is declared, used, changed, and used again with the new value.
Execution Sample
Swift
var x = 5
print(x)
x = 10
print(x)
Declare a mutable variable x, print it, change its value, then print the new value.
Execution Table
StepActionVariable xOutput
1Declare var x = 55
2Print x55
3Change x = 1010
4Print x1010
5End of code10
💡 Code ends after printing the updated value of x.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
xundefined51010
Key Moments - 2 Insights
Why can we change the value of x after declaring it with var?
Because var declares a mutable variable, so x can be reassigned new values as shown in steps 1 and 3 of the execution_table.
What happens if we try to use x before declaring it?
The code would give an error because x is undefined before step 1; variables must be declared before use.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 3?
A5
B10
Cundefined
D0
💡 Hint
Check the 'Variable x' column at step 3 in the execution_table.
At which step does the output first show the value 5?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table to find when 5 is printed.
If we change 'var x = 5' to 'let x = 5', what would happen at step 3?
AError because let variables cannot be changed
Bx changes to 10 successfully
Cx becomes 0
DNo output is printed
💡 Hint
Recall that let declares constants which cannot be reassigned, unlike var.
Concept Snapshot
var declares a mutable variable in Swift.
You can assign a value and later change it.
Syntax: var variableName = value
Changing the value is allowed: variableName = newValue
Use var when you need to update the variable.
Full Transcript
This example shows how to use var in Swift to create a variable that can change. First, we declare x with var and assign 5. Then we print x, which outputs 5. Next, we change x to 10 and print again, outputting 10. The key point is var allows changing the variable's value after declaration. If we used let instead, changing x would cause an error. This helps understand mutable variables in Swift.

Practice

(1/5)
1. What does the var keyword do in Swift?
easy
A. It creates a variable whose value can change later.
B. It creates a constant value that cannot change.
C. It defines a function.
D. It declares a type alias.

Solution

  1. Step 1: Understand the purpose of var

    The var keyword is used to declare variables that can be changed after they are set.
  2. Step 2: Compare with constants

    Unlike let, which creates constants, var allows the value to be updated.
  3. Final Answer:

    It creates a variable whose value can change later. -> Option A
  4. Quick Check:

    var means mutable variable [OK]
Hint: Remember: var means variable, value can change [OK]
Common Mistakes:
  • Confusing var with let (constant)
  • Thinking var creates constants
  • Mixing var with function declaration
2. Which of the following is the correct way to declare a mutable variable named score with initial value 10 in Swift?
easy
A. let score = 10
B. variable score = 10
C. const score = 10
D. var score = 10

Solution

  1. Step 1: Identify the keyword for mutable variables

    In Swift, var declares a variable that can change, so use var score = 10.
  2. Step 2: Check other options

    let creates a constant, variable and const are not valid Swift keywords.
  3. Final Answer:

    var score = 10 -> Option D
  4. Quick Check:

    Mutable variable uses var [OK]
Hint: Use var for variables, let for constants [OK]
Common Mistakes:
  • Using let instead of var for mutable variables
  • Using invalid keywords like variable or const
  • Missing the equals sign
3. What will be the output of this Swift code?
var count = 5
count = 10
print(count)
medium
A. 10
B. 5
C. Error: Cannot assign to constant
D. nil

Solution

  1. Step 1: Understand variable assignment

    The variable count is declared with var and initially set to 5, so it can be changed.
  2. Step 2: Update and print value

    The value is changed to 10 before printing, so print(count) outputs 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Mutable var updates value correctly [OK]
Hint: var allows value change, so print updated value [OK]
Common Mistakes:
  • Thinking var acts like let (constant)
  • Expecting original value instead of updated
  • Confusing syntax causing errors
4. Identify the error in this Swift code:
var name = "Alice"
name = 42
print(name)
medium
A. Syntax error on variable declaration
B. No error, prints 42
C. Type mismatch error when assigning 42 to name
D. Runtime error when printing

Solution

  1. Step 1: Check variable type inference

    Variable name is inferred as String because of initial value "Alice".
  2. Step 2: Assign incompatible type

    Assigning an integer 42 to a String variable causes a type mismatch error.
  3. Final Answer:

    Type mismatch error when assigning 42 to name -> Option C
  4. Quick Check:

    Swift variables keep type, cannot assign different type [OK]
Hint: Variable type fixed by first value, cannot assign different type [OK]
Common Mistakes:
  • Ignoring type mismatch errors
  • Thinking var allows any type changes
  • Confusing syntax error with type error
5. You want to keep track of a player's score that changes during the game. Which Swift code correctly declares and updates this score using var?
1. let score = 0
2. score = 10

3. var score = 0
4. score = 10

5. var score: String
6. score = 10

7. let score: Int
8. score = 10
hard
A. Lines 1 and 2
B. Lines 3 and 4
C. Lines 5 and 6
D. Lines 7 and 8

Solution

  1. Step 1: Identify mutable variable declaration

    Lines 3 and 4 declare score with var and assign 0, then update to 10, which is correct.
  2. Step 2: Check other options

    Lines 1 and 2 use let (constant), so line 2 causes error. Lines 5 and 6 declare score as String but assign integer 10, causing type mismatch error. Lines 7 and 8 use let constant and then try to assign, causing error.
  3. Final Answer:

    Lines 3 and 4 -> Option B
  4. Quick Check:

    Use var to declare and update variables [OK]
Hint: Declare with var and assign before update [OK]
Common Mistakes:
  • Using let for variables that change
  • Type mismatch with wrong type annotation
  • Confusing var and let usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard