0

Can you explain the advantage of stride in Swift and its peculiar use?

e.g.:

for i in stride(from: 0, to: 10, by: 1) {
    print(i)  // prints from 0 to 9
}

Instead of this we can use for loop also.

Akash
  • 91
  • 2
  • 12

1 Answers1

2

Your question appears to be asking about the benefits of stride over a basic for loop with a range such as 1..<10.

stride supports incrementing by values other than 1.

// count by 3
for x in stride(from: 3, to: 30, by: 3) {
}

// Backwards
for n in stride(from: 20, to: 0, by: -4) {
}

stride works with non-integer values.

for r in stride(from: 3.14, to: 234.14234, by: 5.6345) {
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579