0
0
Swiftprogramming~15 mins

Why let and var distinction matters in Swift - See It in Action

Choose your learning style9 modes available
Why let and var distinction matters
📖 Scenario: Imagine you are organizing a small event and need to keep track of the number of guests and the event name. Some information will stay the same, while other information might change.
🎯 Goal: You will create variables and constants using var and let in Swift to understand why the distinction matters.
📋 What You'll Learn
Create a constant called eventName with the value "Birthday Party"
Create a variable called guestCount with the value 10
Increase the guestCount by 5
Print both eventName and guestCount
💡 Why This Matters
🌍 Real World
In real life, some information stays the same, like the event name, while other information changes, like the number of guests. Using constants and variables helps keep track of this clearly in your programs.
💼 Career
Understanding when to use constants and variables is a basic skill for any Swift developer. It helps write reliable and maintainable code, which is important in app development and software engineering jobs.
Progress0 / 4 steps
1
Create the initial data
Create a constant called eventName and set it to "Birthday Party". Create a variable called guestCount and set it to 10.
Swift
Need a hint?

Use let for values that do not change and var for values that can change.

2
Update the guest count
Increase the variable guestCount by 5 using the += operator.
Swift
Need a hint?

Use guestCount += 5 to add 5 to the current guest count.

3
Try to change the event name
Try to change the constant eventName to "Wedding Party". (This will cause an error, but type the line exactly as shown.)
Swift
Need a hint?

This line will cause an error because eventName is a constant and cannot be changed.

4
Print the event name and guest count
Print the values of eventName and guestCount using two separate print statements.
Swift
Need a hint?

Use print(eventName) and print(guestCount) to show the values.