-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray4iteration.swift
More file actions
34 lines (30 loc) · 865 Bytes
/
Copy patharray4iteration.swift
File metadata and controls
34 lines (30 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Itration over an array using a for-in loop
let colors: [String] = ["Red", "Green", "Blue", "Yellow", "Purple"]
print("\nIterating using for-in loop:")
for color in colors {
print(color)
}
// Itration over an array using enumerated()
print("\nIterating using enumerated():")
for (index, color) in colors.enumerated() {
print("Color at index \(index) is \(color)")
}
// Itration over an array using forEach method
print("\nIterating using forEach method:")
colors.forEach { color in
print(color)
}
// Itration over an array using while loop
print("\nIterating using while loop:")
var index = 0
while index < colors.count {
print(colors[index])
index += 1
}
// Itration over an array using repeat-while loop
print("\nIterating using repeat-while loop:")
index = 0
repeat {
print(colors[index])
index += 1
} while index < colors.count