let count = 10
// Triangle numbers
var sum = 0
for i in 1...count {
sum += i
}
sum
// Fibonacci
sum = 1
var lastSum = 0
for _ in 0..<count {
let temp = sum
sum = sum + lastSum
lastSum = temp
}
sum
// Sum odd numbers
sum = 0
for i in 1...count where i % 2 == 1 {
sum += i
}
sum
let number = 10
switch number {
case 0:
print("Zero")
default:
print("Non-zero")
}
switch number {
case 10:
print("It's ten!")
default:
break
}
let string = "Dog"
switch string {
case "Cat", "Dog":
print("Animal is a house pet.")
default:
print("Animal is not a house pet.")
}
switch number {
case _ where number % 2 == 0:
print("Even")
default:
print("Odd")
}
let coordinates = (x: 3, y: 2, z: 5)
// pattern matching
switch coordinates {
case (0, 0, 0):
print("Origin")
case (_, 0, 0):
print("On the x-axis.")
case (0, _, 0):
print("On the y-axis.")
case (0, 0, _):
print("On the z-axis.")
default:
print("Somewhere in space")
}
// pattern matching, binding value
switch coordinates {
case (0, 0, 0):
print("Origin")
case (let x, 0, 0):
print("On the x-axis at x = \(x)")
case (0, let y, 0):
print("On the y-axis at y = \(y)")
case (0, 0, let z):
print("On the z-axis at z = \(z)")
case let (x, y, z):
print("Somewhere in space at x = \(x), y = \(y), z = \(z)")
}
switch coordinates {
case let (x, y, _) where y == x:
print("Along the y = x line.")
case let (x, y, _) where y == x * x:
print("Along the y = x^2 line.")
default:
break
}