ios

Perulangan pada Bahasa Pemrograman Swift

For-In Loop

let names = ["Ihwan", "Devi", "Patrick", "Indah", "Gandy", "Edo"]
for name in names {
    print("Hello, \(name)!")
}
let numberOfAges = ["Ihwan": 17, "Mat Solar": 30, "Ucup Bensin": 25]
for (name, age) in numberOfAges {
    print("\(name) umur adalah \(age)")
}
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}

While Loops

var index = 10

while index < 20 {
   print( "Value of index is \(index)")
   index = index + 1
}

Repeat While Loops

var currentLevel:Int = 0, finalLevel:Int = 5
let gameCompleted = true
repeat {
    //play game
    if gameCompleted {
        print("You have passed level \(currentLevel)")
        currentLevel += 1
    }
} while (currentLevel <= finalLevel)
print("outside of repeat while loop")