0
0
Swiftprogramming~15 mins

Var for variables (mutable) in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Var for variables (mutable)
📖 Scenario: Imagine you are keeping track of the number of apples you have in a basket. Sometimes you add apples, and sometimes you eat some. You want to write a program that can change the number of apples as you go.
🎯 Goal: You will create a variable to hold the number of apples, change its value, and then print the final number of apples.
📋 What You'll Learn
Create a variable called appleCount and set it to 10
Change the value of appleCount to 15
Print the value of appleCount
💡 Why This Matters
🌍 Real World
Variables like <code>appleCount</code> help keep track of changing information, like inventory or scores in games.
💼 Career
Understanding mutable variables is key for programming tasks where data changes over time, such as in app development or data processing.
Progress0 / 4 steps
1
Create a variable with initial value
Create a variable called appleCount and set it to 10 using var.
Swift
Need a hint?

Use var to create a variable that can change later.

2
Change the variable value
Change the value of the variable appleCount to 15.
Swift
Need a hint?

Just write appleCount = 15 to update the variable.

3
Use the variable in a calculation
Add 5 to the current value of appleCount and store the result back in appleCount.
Swift
Need a hint?

Use appleCount = appleCount + 5 to add 5 to the current value.

4
Print the final value
Print the value of appleCount using print().
Swift
Need a hint?

Use print(appleCount) to show the number of apples.