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.
var x = 5 print(x) x = 10 print(x)
| Step | Action | Variable x | Output |
|---|---|---|---|
| 1 | Declare var x = 5 | 5 | |
| 2 | Print x | 5 | 5 |
| 3 | Change x = 10 | 10 | |
| 4 | Print x | 10 | 10 |
| 5 | End of code | 10 |
| Variable | Start | After Step 1 | After Step 3 | Final |
|---|---|---|---|---|
| x | undefined | 5 | 10 | 10 |
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.
var keyword do in Swift?varvar keyword is used to declare variables that can be changed after they are set.let, which creates constants, var allows the value to be updated.var means mutable variable [OK]score with initial value 10 in Swift?var declares a variable that can change, so use var score = 10.let creates a constant, variable and const are not valid Swift keywords.var [OK]var count = 5 count = 10 print(count)
count is declared with var and initially set to 5, so it can be changed.print(count) outputs 10.var name = "Alice" name = 42 print(name)
name is inferred as String because of initial value "Alice".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
score with var and assign 0, then update to 10, which is correct.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.Pass the test and this pattern moves unknown