🏷️ 3. Variables Are Like Name Tags!
Hello! 😊
Today, we’re going to learn about one of the most important ideas in Swift coding — variables!
The word “variable” might sound a little difficult at first,
but it’s actually very simple — it’s just like using a name tag or a labeled box in your everyday life.
Let’s learn together in a fun and easy way!
📦 What Is a Variable?
A variable is like a name tag that stores information.
When we want to remember or reuse a value,
we use a variable to say: “This value has this name!”
For example, let’s say your favorite fruit is an apple.
We can save the word “apple” using the name fruit, like this:
var fruit = "Apple"
• var tells the computer, “This is a variable!”
• fruit is the name of the variable
• "Apple" is the actual value we’re saving
So, this line of code means we’re storing the word “Apple” in a box named fruit. 🍎
🧸 Let’s Label Our Things
Now, let’s try labeling other things around us using variables!
• Your favorite toy 👉 toy
• Your favorite color 👉 favoriteColor
• Your name 👉 myName
• Your age 👉 myAge
Here’s what it looks like in Swift:
var toy = "LEGO"
var favoriteColor = "Blue"
var myName = "Minsu"
var myAge = 10
Now you can use those variables in your code to remember what you saved!
🧪 Practice: Save Your Favorite Fruit in a Variable
Let’s try a little coding activity together!
Open Swift Playground and type this:
var myFavoriteFruit = "Banana"
print("My favorite fruit is \(myFavoriteFruit)!")
When you run the code, the screen will show:
My favorite fruit is Banana!
Pretty cool, right?
Now change the value like this:
myFavoriteFruit = "Grapes"
And try printing again. You’ll see:
My favorite fruit is Grapes!
This shows how variables let you save values, change them, and use them whenever you need!
✨ Wrapping Up
Variables are one of the most important tools in coding.
But don’t worry — they’re not hard at all!
댓글 쓰기