What if your program could remember changes just like you do, without extra effort?
Why Var for variables (mutable) in Swift? - Purpose & Use Cases
Imagine you are writing down your daily expenses on paper. Every time you spend money, you have to erase the old total and write the new one. This takes time and can cause mistakes if you forget to update the number.
Manually updating values is slow and easy to mess up. If you forget to change a number or write it wrong, your whole calculation becomes wrong. It's hard to keep track of changes and fix errors.
Using var in Swift lets you create variables that can change their value easily. You just update the variable, and the program remembers the new value for you. This makes your code flexible and less error-prone.
let total = 10 // To change total, you must create a new constant or do manual recalculation
var total = 10 total = 15 // Just update the variable directly
It allows your program to remember and update information as things change, just like you do in real life.
Tracking your bank account balance where deposits and withdrawals change the total amount over time.
Variables let you store values that can change.
Using var in Swift makes updating values easy and safe.
This helps your programs handle real-world changing data smoothly.