Day 3 of 100 Days of SwiftUI
Tags: swift, swiftui, beginners
Published: 2020-01-17 22:54:23

#Introduction

This is the third day of learning [SwiftUI](https://developer.apple.com/xcode/swiftui/) using [Paul Hudson](https://twitter.com/twostraws)'s [100 Days of SwiftUI challenge](https://www.hackingwithswift.com/100/swiftui). 

Today I learned about <strong>Operators and Conditions</strong>  

#Summary
Please find below a summary of my jottings on these topics
<ul>
  <li>
    <strong id ="array">Operators</strong> - We have addition, substraction, multiplication, division, and modulus.
  </li>
</ul>
``` swift
let testScore = 45
let examScore = 30

let addition = testScore + examScore

let substraction = testScore - examScore

let multiply = testScore * examScore

let division = 50 / 5

// Returns the remainder after a division operation
let modulus = 15 % 6
```

<ul>
  <li>
    <strong id ="set">Operator overloading</strong> - This means that what an operator does is dependant on the values used with it.
  </li>
</ul>
``` swift
// Addition
let result = 10 + 10

// String concatenation (as it is called in JavaScript)
let firstPart = "Welcome home "
let finalPart = "Africans!"
let welocmeMessage = firstPart + finalPart

// We can even join arrays together using + operator
let initialArray = ["East", "West"]
let additionalArray = ["North", "South"]
let cardinalPoint = initialArray + additionalArray
```

<ul>
  <li>
    <strong id ="dictionary">Compound operators</strong> - They assign the result back to whatever variable you were using.
  </li>
</ul>
``` swift
var score = 70
let attendanceMark = 10

score += attendanceMark

// Similarly, you can add one string to another using +=
var fact = "Nigeria is the giant of "
fact += "Africa"

```
<ul>
  <li>
    <strong id ="tuple">Comparison operators</strong> - These includes equality operators, inequality operators, greater than, and less than.
  </li>
</ul>
``` swift
// comparison operators
let first =  19
let second = 23

// equality operators: checks two values are the same
first == second

// inequality operators: checks two values are not the same
first != second

// greater than, greater than or equal to
first > second
first >= second

// less than, less than or equal to
first < second
first <= second

// Each of these operators also works with strings, because strings have a natural alphabetical order
// Prints true
"a" < "b"
// Prints false
"Africa" > "America"
```

<ul>
  <li>
    <strong id ="condition">Conditions</strong> - You give Swift a condition, and if that condition is true it will run the code you instruct it.</li>
</ul>
``` swift
let firstAge =  50
let secondAge = 50

// if the condition is true, the code in the bracket will run
if firstAge > secondAge {
    // Swift built-in function to print output
    print("The first dude is older.")
} // You can chain conditions together using else if
else if (firstAge == secondAge) {
    print("Wow, you guys are mate!")
}
//  If you want you can provide alternative code to run if the conditions are false, using else
else {
    print("The second dude is older.")
}

// Combining conditions -  && (pronounced “and”) and || (pronounced “or”) are used to combine conditions together
let x = 9
let y = 2

// &&
if (x > 1 && y > 1) {
    print("The two conditions are met")
}

// ||
if (x > 1 || y > 10) {
    print("One of the conditions is correct")
}

// Use case
let loggedIn = true
let authorized = false

if loggedIn && authorized {
    print("Welcome to Wakanda!")
}
```

<ul>
  <li>
    <strong id ="condition">Ternary operator</strong> - The ternary operator is a condition plus true or false blocks all in one, split up by a question mark and a colon, all of which which makes it rather hard to read.</li>
</ul>
``` swift
let firstCard = 11
let secondCard = 10
// ternary operator
print(firstCard == secondCard ? "Cards are the same" : "Cards are different")

// This code is same as an if..else statement
if firstCard == secondCard {
    print("Cards are the same")
} else {
    print("Cards are different")
}
```

<ul>
  <li>
    <strong id ="condition">Switch</strong> - You write your condition once, then list all possible outcomes and what should happen for each of them.</li>
</ul>
``` swift
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!")
}

// If you want execution to continue on to the next case, use the fallthrough
switch weather {
case "rain":
    print("Bring an umbrella")
case "snow":
    print("Wrap up warm")
case "sunny":
    print("Wear sunscreen")
    fallthrough
    // the default case must be there to ensure all possible values are covered.
default:
    print("Enjoy your day!")
}
```

<ul>
  <li>
    <strong>Range Operators</strong> - "The half-open range operator" ..< creates ranges up to but excluding the final value. "The closed range operator" ... creates ranges up to and including the final value. </li>
</ul>
``` swift
let studResult = 85

switch studResult {
case 0..<50:
    print("You failed badly.")
case 50..<85:
    print("You did OK.")
default:
    print("You did great!")
}

for i in 1...100 {
   print("Currently on Day \(i)")
}

for i in 1..<8 {
   print("Currently on Day \(i)")
}
```
Thanks for reading🙏🏿.

You can find me on Twitter [@RealKennyEdward](https://twitter.com/RealKennyEdward)

I'm on to Day4!