I have an array of 100 items. I want to iterate it with specific steps, 2n, 3n, 4n etc. For example, if n = 3, i want to iterate 3,6,9,12 elements of array.
How to achieve that? Thanks.
I have an array of 100 items. I want to iterate it with specific steps, 2n, 3n, 4n etc. For example, if n = 3, i want to iterate 3,6,9,12 elements of array.
How to achieve that? Thanks.
In Swift you can do this with help of stride
.
let n = 3
for index in stride(from: 0, through: 100, by: n) {
print(index)
}
Output of the indexes:
0
3
6
9
12
15
18
21
24
27
30
33
36
39
42
...
I dont know swift but by the look of it from the basic swift loops this should do exactly what you want :
var i = 1
var n = 2
while i <= 100 {
print(i)
i = i + n
}