On C-Style For Loops Removed from Swift 3
On C-Style For Loops Removed from Swift 3.0
Recently, Erica Sadun had submitted a proposal to the Swift open source repository suggesting the removal of C-style for loops from Swift 3.0. This came after the announcement that unary increment and decrement operators would be removed (++ and --, respectively). While this is a shock to many, I think this is a great move, in my opinion, to show that Swift won’t just have every operator we’re all used to just because they’ve always been in most any other language. Some may ask why have it in the first place, then take it away? Here’s how I feel about this all.
(Note: When searching for references for links, I found Erica’s latest article is also about for-in iteration tricks. This is purely coincidental, as my desire to write about it came from a recent conversation with other developers. This article and hers appear to be good companion articles to each other)
Short Answer
I’m for it. (yep, pun intended)
Long Answer
I’m still for it. Lately, I’ve heard a lot of people not so happy about this move. The biggest reason I hear is that they understand that fast enumeration is a great alternative (i.e. for item in items {…} ), but they want to have the index available sometimes while iterating.
Indexing While Iterating
We’re all used to the tried-and-true C-style for loops, such as the one in the following Figure 1.

Figure 1
This is pretty standard, but this is what is being removed. An alternative is to use fast enumeration with a for-in loop, such as in the code in Figure 2.

Figure 2
Yep, no index. Sometimes, it’s not necessary.
Incrementing
But, sometimes it is necessary. Figure 3 shows how to use fast enumeration with ranges, which is the best way that I can see to get the index while using fast enumeration.

Figure 3
That’s great for incrementing, but what about decrementing, like i-- for example?
Decrementing
Utilizing the reverse() function on a range will decrement specified range. Figure 4 shows how to reverse a range using fast enumeration to accomplish what a C-style for loop would do with i-- as the third operation in the for loop’s declaration.

Figure 4
Other Methods of Enumeration
(This is an update: thanks to Daniel Steinberg for the reminder) You can also use the enumerate() function on any sequence that is a SequenceType to get the index along with the current item. Figure 5 illustrates how to do this.

Figure 5
Ok, so we’ve got incrementing and decrementing, but what if I need to increment or decrement by 2, or some other number?
Non-sequential Indexing
Swift has some pretty advanced pattern matching, and you can even use that matching inside for loops, as well as many other places. If you need to iterate through an array, knowing the index, but incrementing by 2 per se, Figure 6 illustrates how you could do that by using the where clause.

Figure 6
Using the where clause here is akin to using i += 2 in the last for loop expression.
Striding To or Through
Another way to enumerate in a non-sequential format is to use the stride function. You can either stride to or stride through a number, which can be thought of almost like ..< and ... respectively in that stride to goes up to but not including its final number, whereas stride through does include its final number. Figure 7 illustrates the stride(to:, by:) function which allows you to specify the increment/decrement amount.

Figure 7
Conclusion
Again, I’m for it. In addition to methods shown here, there are also others that are not listed, but this should give you a good idea of what you can do with loops without needing the original C-style for loops. I think that C-style for loops can be difficult to grasp at first for beginners, and this “new” Swift style of looping is a tad more consistent with other Swift-style loops, functions, and syntax in my opinion. I’m glad to see Apple taking care to ensure that each function, operator, and so on, get examined as to their existence in the language. It shows that they really want to make this a great language.