[Swift]: for loop

swift

05/11/2019


Different usage for Loops in Swift

1) for in for countable ranges

SWIFT
for _ in 0..<numPairs {
let card = Card()
}
SWIFT
0..<numPairs

It does not include numPairs (exclusive). Use ... for inclusive Use _ when we don't care about what identifier is. i.e. when you don't need a particular identifier that increases

2) Floating point CountableRange

stride global function can be used for floating point values

SWIFT
for i in stride(from: 0.5, through: 9.80, by: 0.3) {
// Code
}

The return type of stride is a ClosedCountableRange because through: is inclusive.
On the other hand, use of to:, inclusive, will return CountableRange, a generic type (doesn't have to be int).


WRITTEN BY

Keeping a record