Completed 100 Days of Swift - Day #3
This commit is contained in:
parent
4308181834
commit
350de9bc52
|
@ -2,7 +2,7 @@
|
|||
|
||||
---
|
||||
|
||||
**What I learned**
|
||||
**What I learned about**
|
||||
|
||||
- **[Variables](/100DaysOfSwift/Day1/Variables.swift)**
|
||||
- **[Strings & Integers](/100DaysOfSwift/Day1/Strings%20%26%20Integers.swift)**
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
---
|
||||
|
||||
**What I learned**
|
||||
**What I learned about**
|
||||
|
||||
- **[Arrays](/100DaysOfSwift/Day2/Arrays.swift)**
|
||||
- **[Sets](/100DaysOfSwift/Day2/Sets.swift)**
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
// Arithmetic operators
|
||||
// https://www.hackingwithswift.com/sixty/3/1/arithmetic-operators
|
||||
|
||||
let firstScore = 12
|
||||
let secondScore = 4
|
||||
|
||||
let total = firstScore + secondScore
|
||||
let difference = firstScore - secondScore
|
||||
|
||||
let product = firstScore * secondScore
|
||||
let divided = firstScore / secondScore
|
||||
|
||||
let remainder = 13 % secondScore
|
|
@ -0,0 +1,13 @@
|
|||
// Combining conditions
|
||||
// https://www.hackingwithswift.com/sixty/3/6/combining-conditions
|
||||
|
||||
let age1 = 12
|
||||
let age2 = 21
|
||||
|
||||
if age1 > 18 && age2 > 18 {
|
||||
print("Both are over 18")
|
||||
}
|
||||
|
||||
if age1 > 18 || age2 > 18 {
|
||||
print("At least one is over 18")
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
// Comparison operators
|
||||
// https://www.hackingwithswift.com/sixty/3/4/comparison-operators
|
||||
|
||||
let firstScore = 6
|
||||
let secondScore = 4
|
||||
|
||||
// There are two operators that check for equality: == checks two values are the same, and != (pronounced “not equals”) checks two values are not the same:
|
||||
firstScore == secondScore
|
||||
firstScore != secondScore
|
||||
|
||||
// There are four operators for comparing whether one value is greater than, less than, or equal to another. These are just like in mathematics:
|
||||
firstScore < secondScore
|
||||
firstScore >= secondScore
|
||||
|
||||
// Each of these also work with strings, because strings have a natural alphabetical order:
|
||||
"Taylor" <= "Swift"
|
|
@ -0,0 +1,8 @@
|
|||
// Compound assignment operators
|
||||
// https://www.hackingwithswift.com/sixty/3/3/compound-assignment-operators
|
||||
|
||||
var score = 95
|
||||
score -= 5
|
||||
|
||||
var quote = "The rain in Spain falls mainly on the "
|
||||
quote += "Spaniards"
|
|
@ -0,0 +1,27 @@
|
|||
// Conditions
|
||||
// https://www.hackingwithswift.com/sixty/3/5/conditions
|
||||
|
||||
let firstCard = 11
|
||||
let secondCard = 10
|
||||
|
||||
/*
|
||||
if firstCard + secondCard == 21 {
|
||||
print("Blackjack!")
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
if firstCard + secondCard == 21 {
|
||||
print("Blackjack!")
|
||||
} else {
|
||||
print("Regular cards")
|
||||
}
|
||||
*/
|
||||
|
||||
if firstCard + secondCard == 2 {
|
||||
print("Aces – lucky!")
|
||||
} else if firstCard + secondCard == 21 {
|
||||
print("Blackjack!")
|
||||
} else {
|
||||
print("Regular cards")
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
// Operator overloading
|
||||
// https://www.hackingwithswift.com/sixty/3/2/operator-overloading
|
||||
|
||||
let meaningOfLife = 42
|
||||
let doubleMeaning = 42 + 42
|
||||
|
||||
let fakers = "Fakers gonna "
|
||||
let action = fakers + "fake"
|
||||
|
||||
let firstHalf = ["John", "Paul"]
|
||||
let secondHalf = ["George", "Ringo"]
|
||||
let beatles = firstHalf + secondHalf
|
|
@ -0,0 +1,15 @@
|
|||
# Day #3
|
||||
|
||||
---
|
||||
|
||||
**What I learned about**
|
||||
|
||||
- **[Arithmetic operators](/100DaysOfSwift/Day3/Arithmetic%20operators.swift)**
|
||||
- **[Operator overloading](/100DaysOfSwift/Day3/Operator%20overloading.swift)**
|
||||
- **[Compound assignment operators](/100DaysOfSwift/Day3/Compound%20assignment%20operators.swift)**
|
||||
- **[Comparison operators](/100DaysOfSwift/Day3/Comparison%20operators.swift)**
|
||||
- **[Conditions](/100DaysOfSwift/Day3/Conditions.swift)**
|
||||
- **[Combining conditions](/100DaysOfSwift/Day3/Combining%20conditions.swift)**
|
||||
- **[The ternary operator](/100DaysOfSwift/Day3/The%20ternary%20operator.swift)**
|
||||
- \*\*[Switch statements](/100DaysOfSwift/Day3/Switch%20statements.swift)
|
||||
- **[Range operators](/100DaysOfSwift/Day3/Range%20operators.swift)**
|
|
@ -0,0 +1,13 @@
|
|||
// Range operators
|
||||
// https://www.hackingwithswift.com/sixty/3/9/range-operators
|
||||
|
||||
let score = 85
|
||||
|
||||
switch score {
|
||||
case 0..<50:
|
||||
print("You failed badly.")
|
||||
case 50..<85:
|
||||
print("You did OK.")
|
||||
default:
|
||||
print("You did great!")
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
// Switch statements
|
||||
// https://www.hackingwithswift.com/sixty/3/8/switch-statements
|
||||
|
||||
let weather = "sunny"
|
||||
|
||||
/*
|
||||
switch weather {
|
||||
case "rain":
|
||||
print("Bring an umbrella")
|
||||
case "snow":
|
||||
print("Wrap up warm")
|
||||
case "sunny":
|
||||
print("Wear sunscreen")
|
||||
default:
|
||||
print("Enjoy your day!")
|
||||
}
|
||||
*/
|
||||
|
||||
switch weather {
|
||||
case "rain":
|
||||
print("Bring an umbrella")
|
||||
case "snow":
|
||||
print("Wrap up warm")
|
||||
case "sunny":
|
||||
print("Wear sunscreen")
|
||||
fallthrough
|
||||
default:
|
||||
print("Enjoy your day!")
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
// The ternary operator
|
||||
// https://www.hackingwithswift.com/sixty/3/7/the-ternary-operator
|
||||
|
||||
// Swift has a rarely used operator called the ternary operator. It works with three values at once, which is where its name comes from: it checks a condition specified in the first value, and if it’s true returns the second value, but if it’s false returns the third value. The ternary operator is a condition plus true or false blocks all in one, split up by a question mark and a colon
|
||||
|
||||
let firstCard = 11
|
||||
let secondCard = 10
|
||||
print(firstCard == secondCard ? "Cards are the same" : "Cards are different")
|
||||
|
||||
// Regular operator version.
|
||||
|
||||
if firstCard == secondCard {
|
||||
print("Cards are the same")
|
||||
} else {
|
||||
print("Cards are different")
|
||||
}
|
|
@ -11,4 +11,5 @@
|
|||
**Days 1-12: Introduction to Swift**
|
||||
|
||||
- **[Day #1](/100DaysOfSwift/Day1/)** - variables, simple data types, and string interpolation
|
||||
- **[Day #2](/100DaysOfSwift/Day2/)** - arrays, dictionaries, sets, and enums
|
||||
- **[Day #2](/100DaysOfSwift/Day2/)** - arrays, dictionaries, sets, and enums
|
||||
- **[Day #3](/100DaysOfSwift/Day3/)** - operators and conditions
|
Loading…
Reference in New Issue