✅ 7. True or False (Bool)
Hello! 😊, In this lesson, we’re going to learn about something called Bool in Swift.
A Bool is a special data type that can only be true or false.
Think about questions like:
• Did I do my homework?
• Is it nighttime right now?
If you can answer with yes or no, or true or false,
then you’re using the Bool concept!
🤔 What Does True or False Mean?
In Swift, a Bool can only have one of two values:
• true — yes, correct, or it’s happening
• false — no, wrong, or it’s not happening
Here’s an example:
var isSunny = true
var isRaining = false
• isSunny is true → the sun is out! ☀️
• isRaining is false → it’s not raining! ☔
💡 Don’t forget: true and false must be written in lowercase!
🧪 Practice: Did I Do My Homework Today?
Let’s create a small app that tells us whether or not we did our homework today!
var didHomework: Bool = true
if didHomework {
print("Great job! You did your homework 🎉")
} else {
print("Oops! You haven’t done your homework yet 😅")
}
Output:
Great job! You did your homework 🎉
Now change the value:
didHomework = false
You’ll get this output:
Oops! You haven’t done your homework yet 😅
✅ When Do We Use Bool?
Bool values are used when we want to check a condition,
and choose what the program should do next.
Some examples:
• Is the door open? → isDoorOpen = true
• Is the user logged in? → isLoggedIn = false
• Is the score 90 or more? → score >= 90 → result is true or false!
✨ Wrapping Up
Today, we learned about the Bool type in Swift:
• true = yes, correct
• false = no, not true
• Bools are used with if statements to make decisions
Now you can answer questions like “Is this true?” using Swift code — awesome! 😄
In the next lesson, we’ll learn how to repeat actions using something called a loop.
The topic will be: 🔁 Repeating with For Loops!
댓글 쓰기