www.robkerr.com
www.robkerr.com

mobile development, cloud computing and building great software

Rob Kerr
Author

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Share


Tags


Twitter


Swift 3.0 for loop syntax

Rob KerrRob Kerr

If you've been programming for a while, whether you started with K&R C (like me!), or even C++ or Java, you're probably used to the following C-based for loop syntax:

for (int i=0; i<10; i++) {
    printf("%d", i);
}

// printf() is called 10 times

Since most of my coding is in Swift for the last few years, I've been adjusting to its more expressive syntax of the for loop. Now that the old "classic" for with semicolon syntax will start to be unsupported in Swift, I'm putting the real pedal to the metal on learning all the new options!

So for my reference (and yours, if you like) here are the things I need to bang to rote memory:

Old timey for loop (K&R C - ish)

for i in stride(from: 0, to: 10, by: 1) {
    print("\(i)")
}
// print() is called 10 times

Ok, this is using the stride function, which I don't see a lot when googling around for for loop syntax. But I actually kind of like it. Yes, it's a little on the verbose side. But it really tells you exactly what's going on, doesn't it?

In classic C syntax, the most commonly used increment expression is i++, which means "increment by 1". But sometimes you want to advance by other than 1 discrete digit, such as:

for (int i=0; i<10; i += 2) {
    printf("%d", i);
}
// printf() called 5 times

In swift, this is accomplished by swapping to: with through:

for i in stride(from: 1, through: 10, by: 2) {
    print(i)
}
// print() is called 5 times

For-In syntax

Ok, so I do like the stride. But like most, I use the more concise For-In syntax more often. And unlike the old "comfortable pair of jeans" K&R C semicolon syntax, it's shorter and makes more sense expressively:

for i in 0..<10 {
    print("\(i)")
}
// print() is called 10 times

You have to admit that's a lot less typing than K&R C, huh?

This syntax also can handle an inclusive loop (also running on the end number), by just swapping the < for a .:

for i in 0...10 {
    print("\(i)")
}
// print() is called 11 times

Personally, I find ... a little weird, but I'm getting used to it. In English grammer it should be .., which is an inclusive list, but if that were true for this syntax there would be loads of mistakes caused when ..< was intended, but someone forgot to type the < at the end. So this seems a practical necessity.

Iterating over an array

I find the most common thing I do with this kind of for loop iteration is iterating over arrays. This has been true ever since the old C days, and it's just as true with Swift. Mercifully, the Swift language provides a syntax just for this senario:

let intArray = [1, 3, 5, 6, 7, 333, 33_223]

for i in intArray {
    print("\(i)")
}
// print() is called 7 times

This works for any kind of array, and is just a lovely way to walk an array.

Rob Kerr
Author

Rob Kerr

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Comments