Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AhmedMenaim/436d57e3bae41cb6b7a53a04e3ccf023 to your computer and use it in GitHub Desktop.
Save AhmedMenaim/436d57e3bae41cb6b7a53a04e3ccf023 to your computer and use it in GitHub Desktop.
Menaim Academy - Swift Course - Operators Part II
import Foundation
// MARK: - Comparison Operators < > == != >= <=
let number = 8
if number >= 5 { // if number = 5 or number > 5
print(number)
}
else if number < 5 { // if number < 5
print(number)
}
else if number == 8 {
print(2 + number)
}
// MARK: - Ternary Conditional Operator
let answered = false
//if answered {
// print("True")
//}
//else {
// print("False")
//}
answered ? print("True") : print("False") // if answer == true -> print("True") else -> print("False")
// MARK: - Nil-Coalescing Operator ( Default Value ) -> ??
var name: String? // if name != nil -> print(name) else -> print("Menaim")
print(name ?? "Menaim")
// MARK: - Logical Operators -> && = And || = OR ! = NOT
let bread = "Bread"
let milk = "Milk"
let egg = "Egg"
if (bread == "Bread" && milk == "milk") { // Mandatory to have both cases true
print("Items Found")
}
else if (bread == "bread" || milk == "milk") { // if any case is true it will work
print("only one item Found")
}
else if ( (bread == "Bread" || milk == "milk") && (bread == "Bread" || egg == "egg") ) {
print("Items Bought")
}
if !answered {
print("\(!answered)")
}
// MARK: - Ranges -> In Loops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment