Day 3 of HackingWithSwift's 100 days of SwiftUI. Today I learnt about the different types of operators and conditions in Swift.
Swift has the standard number of operators for performing different arithmetic operations. For conditional operators in swift there are three kinds:
- if else condition
- Switch condition
- Tenary condition
1.The if-else condition is used to perform an action if a condition is true. For example, if-else conditions are written like this in Swift.
let level = 9001
if level > 9000 {
print("It's over 9000! You can proceed with your quest.")
} else {
print("Bummer, you definitely need to go for a training arc")
}
The code above checks if the user level is over 9000. If it's over 9000, the user can continue with their quest. If it's not over 9000, they have to go for a training arc.
2.The switch condition is used whenever you want to compare different conditions. With if-else, you are most times comparing two or three conditions. If you have more than three conditions to check, I would suggest you use the switch condition. For example, switch conditions are written like this in Swift.
let level = 10000
switch level {
case 7000 :
print("Muda Muda Muda Muda Muda Muda")
case 8000 :
print("You are not strong enough")
case 9000 :
print("You are strong enough! You can proceed with your quest")
case 10000:
print("It's over 9000! You can proceed with your quest.")
default :
print("Please try again")
}
The code above checks the user level for different values then prints the correct response. Please note switch statements have to be exhaustive. That means they must cover all conditions. That's what the default does in the code. If the user level is not 7000, 8000, 9000, or 10000, it will print "Please try again"
3.The ternary operator is a shorthand for a simple if-else condition. For example, in swift, the ternary conditions are written like this
let level = 9000
print( level > 9000 ? "It's over 9000! You can proceed with your quest." : print("Bummer, you definitely need to go for a training arc")
The code above also checks if the user lever is greater than 9000. If it is greater than 9000 then the print statement after the '?' would be executed. If it is not greater than 9000 then print the statement after the ':' would be executed.
There's also another kind of operator. They are called range operators. They are a bit different from the conditional and arithmetic operators. in swift, range operators are written like this ..<
and ...
..<
is used to create a range from an initial value up to but not including the final value.
...
is used to create a range from an initial value up to including the final value.
For example, we could update our code to use the range operators thereby increasing its precision.
switch level {
case 1000..<3000 :
print("Muda Muda Muda Muda Muda Muda")
case 3000...7000 :
print("You need to go for a training arc.")
case 8000 :
print("You are almost there.")
case 9000 :
print("You are strong enough! You can proceed with your quest")
case 10000...:
print("It's over 9000! You can proceed with your quest.")
default :
print("Please try again")
}
The code above now checks if the level is:
- between 1000 but not up 3000.
- between 3000 and up to 7000
- more than 10000
If you are interested in taking the challenge, you can find it at https://www.hackingwithswift.com/100/swiftui
See you tomorrow ;)
Top comments (0)