Please enable JavaScript.
Coggle requires JavaScript to display documents.
Day 3 (Arithmetic Operators (What is it? (Arithmetic Operators are…
Day 3
Arithmetic Operators
What is it?
Arithmetic Operators are operators equals from mathematics. + to sum, - to subtract, * to multiply, / to divide and % to get rest of division. Typically used to do calculus.
-
-
-
-
-
Operator overloading
What is it?
Operator overloading is used to join Strings and add items to arrays and other collections. Apparently, the operator + is like of mathematics, but the goal is different.
-
+ sample2:
let firstHalf = ["John", "Paul"]
let secondHalf = ["George", "Ringo"]
let beatles = firstHalf + secondHalf
Compound Operators
What is it?
Swift has shorthand operators that combine one operator with an assignment, so you can change a variable in place. These look like the existing operators you know – +, -, *, and /, but they have an = on the end because they assign the result back to whatever variable you were using.
sample:
var score = 95
score -= 5
var quote = "The rain in Spain falls mainly on the "
quote += "Spaniards"
Comparison Operators
What is it?
Swift has several operators that perform comparison, and these work more or less like you would expect in mathematics.
sample
let firstScore = 6
let secondScore = 4
firstScore == secondScore
firstScore != secondScore
firstScore < secondScore
firstScore >= secondScore
"Taylor" <= "Swift"
Conditions
What is it?
Now you know some operators you can write conditions using if statements. You give Swift a condition, and if that condition is true it will run code of your choosing.
sample
let firstCard = 11
let secondCard = 10
if firstCard + secondCard == 21 {
print("Blackjack!")
} else {
print("Regular cards")
}
Combining conditions
What is it?
Swift has two special operators that let us combine conditions together: they are && (pronounced “and”) and || (pronounced “or”).
-
Ternary operator
What is it?
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 its false returns the third value.
sample
let firstCard = 11
let secondCard = 10
print(firstCard == secondCard ? "Cards are the same" : "Cards are different")
Switch
What is it?
If you have several conditions using if and else if, it’s often clearer to use a different construct known as switch case. Using this approach you write your condition once, then list all possible outcomes and what should happen for each of them.
-
Range
What is it?
Swift gives us two ways of making ranges: the ..< and ... operators. The half-open range operator, ..<, creates ranges up to but excluding the final value, and the closed range operator, ..., creates ranges up to and including the final value.
-
-