//for-in di dalam arrya
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!
//for-in di dalam dictionary
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
// cats have 4 legs
// ants have 6 legs
// spiders have 8 legs
//index & (_)
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
for let index in 1...5 {
...
}
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
//stride(from:to:by:) & (..<)
print("\(base) to the power of \(power) is \(answer)")
// Prints "3 to the power of 10 is 59049
let minutes = 60
for tickMark in 0..<minutes {
// enggambar tanda centang setiap menit (60 kali)
}
//stride(from:through:by)
let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
// menggambar tanda centang setiap 5 menit sekali (0, 5, 10, 15 ... 45, 50, 55)
}
let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
// menggambar tanda centang setiap 3 jam sekali(3, 6, 9, 12)
}