Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CONDITIONS
- let firstCard = 11
- let secondCard = 10
- if firstCard + secondCard == 2 {
- print("Aces – lucky!")
- } else if firstCard + secondCard == 21 {
- print("Blackjack!") // Console will print "Blackjack!"
- } else {
- print("Regular cards")
- }
- // Combing Conditions
- let age1 = 12
- let age2 = 21
- if age1 > 18 && age2 > 18 { // Use || for "or"
- print("Both are over 18")
- }
- // TERNARY OPERATOR
- let card1 = 11
- let card2 = 10
- print(firstCard == secondCard ? "Cards are the same" : "Cards are different") // "Cards are different"
- // SWITCH STATEMENTS
- let weather = "rain"
- switch weather {
- case "rain":
- print("Bring an umbrella")
- case "snow":
- print("Wrap up warm")
- case "sunny":
- print("Wear sunscreen")
- fallthrough // Used if you want the code to examine the next case
- default:
- print("Enjoy your day!")
- }
- // RANGE OPERATORS
- // Half-open range operator: ..<
- // Closed range operator: ...
- let score = 85
- switch score {
- case 0..<50:
- print("You failed badly.")
- case 50..<85:
- print("You did OK.")
- default:
- print("You did great!")
- }
Advertisement
Add Comment
Please, Sign In to add comment