If-else allows duplicate criteria whereas switch does not. This is the main difference between them
enumWeather {case sun, rain, wind, snow, unknown}let forecast = Weather.rainif forecast == .sun {print("It should be a nice day.")} elseif forecast == .rain {print("Pack an umbrella.")} elseif forecast == .wind {print("Wear something warm")} elseif forecast == .rain { // dublicated criteria, if-else allows but switch does notprint("School is cancelled.")} else {print("Our forecast generator is broken!")}switch forecast {case .sun:print("It should be a nice day.")case .rain:print("Pack an umbrella.")case .wind:print("Wear something warm")case .snow:print("School is cancelled.")case .unknown:print("Our forecast generator is broken!")}
default:
In some cases, you will have dozens if not hundreds of criteria, in those cases normally you will need to describe all of them in switch formula. But swift does not require it, and with the help of "default:" case, it allows you to describe a result for all other cases at once.
let place ="Metropolis"switch place {case"Gotham":print("You're Batman!")case"Mega-City One":print("You're Judge Dredd!")case"Wakanda":print("You're Black Panther!")default:print("Who are you?")}/*print:Who are you?*/
The "default:" criteria must be at the end, otherwise swift will throw an error message, "Additional 'case' blocks cannot appear after the 'default' block of a 'switch'"
switch place {default:// this is the default value for all other casesprint("Who are you?")// compile: Additional 'case' blocks cannot appear after the 'default' block of a 'switch'case"Gotham":print("You're Batman!")case"Mega-City One":print("You're Judge Dredd!")case"Wakanda":print("You're Black Panther!")}
fallthrough
In some cases, you may need to merge multiple cases in order.
"fallthrough" Allows you to merge multiple (ordered) criteria. You can use it as many times as you want.
let day =5print("My true love gave to me…")switch day {case5:print("5 golden rings")fallthroughcase4:print("4 calling birds")fallthroughcase3:print("3 French hens")fallthroughcase2:print("2 turtle doves")fallthroughdefault:print("A partridge in a pear tree")}/*print:5 golden rings4 calling birds3 French hens2 turtle dovesA partridge in a pear tree*/// --switch day {case5:print("5 golden rings")fallthroughcase4:print("4 calling birds")fallthroughcase3:print("3 French hens")// fallthroughcase2:print("2 turtle doves")// fallthroughdefault:print("A partridge in a pear tree")}/*print:5 golden rings4 calling birds*/// it did not print the "default:"
Ternary Conditional Operator
Criteria ? :
"criteria": > || < || = || ! || !=
"?": True
":" False
This squence also called as WTF
W: What to check
T: if True
F: if False
let age =18let canVote = age >=18?"Yes":"No"print(canVote)// print: "Yes"// More simple versionprint(age >=18?"Yes":"No")// print: "Yes"
You cannot insert an "if-else" statement in a "print", you can only insert "print" in an "if-else".
The other solution which is more readable is to use a ternary in a "print". #control