importCocoa// ## Conditions// ifvarusername=""ifusername.count==0{print("empty")}else{print("not empty")}username="john doe"ifusername.isEmpty{print("empty")}else{print("not empty")}// if - else if - elseleta=falseletb=trueifa{print("Code to run if a is true")}elseifb{print("Code to run if a is false but b is true")}else{print("Code to run if both a and b are false")}// && - and, || - or/*
if temp > 20 && temp < 30 {}
if userAge >= 18 || hasParentalConsent == true {}
*/// ## switch - caseletplace="Metropolis"// You must list all outcomes as case statements or use default.switchplace{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?")}/*
Remember: Swift checks its cases in order and runs the first one that matches.
If you place default before any other case, that case is useless
because it will never be matched and Swift will refuse to build your code.
Second, if you explicitly want Swift to carry on executing subsequent cases,
use `fallthrough`. This is not commonly used.
*/letday=5print("My true love gave to me…")switchday{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")}// ## Ternaryletage=18letcanVote=age>=18?"Yes":"No"print(canVote)
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)