GregLeck

Operators and conditions

Sep 25th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.09 KB | None | 0 0
  1. // CONDITIONS
  2. let firstCard = 11
  3. let secondCard = 10
  4. if firstCard + secondCard == 2 {
  5.     print("Aces – lucky!")
  6. } else if firstCard + secondCard == 21 {
  7.     print("Blackjack!") // Console will print "Blackjack!"
  8. } else {
  9.     print("Regular cards")
  10. }
  11.  
  12. // Combing Conditions
  13. let age1 = 12
  14. let age2 = 21
  15. if age1 > 18 && age2 > 18 {  // Use || for "or"
  16.     print("Both are over 18")
  17. }
  18.  
  19. // TERNARY OPERATOR
  20. let card1 = 11
  21. let card2 = 10
  22. print(firstCard == secondCard ? "Cards are the same" : "Cards are different") // "Cards are different"
  23.  
  24. // SWITCH STATEMENTS
  25. let weather = "rain"
  26. switch weather {
  27. case "rain":
  28.     print("Bring an umbrella")
  29. case "snow":
  30.     print("Wrap up warm")
  31. case "sunny":
  32.     print("Wear sunscreen")
  33.     fallthrough // Used if you want the code to examine the next case
  34. default:
  35.     print("Enjoy your day!")
  36. }
  37.  
  38. // RANGE OPERATORS
  39. // Half-open range operator: ..<
  40. // Closed range operator: ...
  41. let score = 85
  42. switch score {
  43. case 0..<50:
  44.     print("You failed badly.")
  45. case 50..<85:
  46.     print("You did OK.")
  47. default:
  48.     print("You did great!")
  49. }
Advertisement
Add Comment
Please, Sign In to add comment