DEV Community

Odewole Kehinde
Odewole Kehinde

Posted on • Updated on

Day 3 of 100 Days of SwiftUI

Introduction

This is the third day of learning SwiftUI using Paul Hudson's 100 Days of SwiftUI challenge.

Today I learned about Operators and Conditions

Summary

Please find below a summary of my jottings on these topics

  • Operators - We have addition, substraction, multiplication, division, and modulus.
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
  • Operator overloading - This means that what an operator does is dependant on the values used with it.
// 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
  • Compound operators - They assign the result back to whatever variable you were using.
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"

  • Comparison operators - These includes equality operators, inequality operators, greater than, and less than.
// 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"
  • Conditions - You give Swift a condition, and if that condition is true it will run the code you instruct it.
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!")
}
  • Ternary operator - 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.
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")
}
  • Switch - You write your condition once, then list all possible outcomes and what should happen for each of them.
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!")
}
  • Range Operators - "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.
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

I'm on to Day4!

Discussion (0)