Rewriting typical C-Style for statements toward Swift 3.0

# C-Style `for` statement has been deprecated.

As you know C-Style `for` statement has been deprecated, and will be removed in Swift 3.0.  Then I wrote an article about how to rewrite your C-Style `for` statements into new Swift 3.0 ready style, based on typical scenarios.

<img width="633" alt="Screen Shot 2016-04-08 at 11.55.56 AM.png" src="https://qiita-image-store.s3.amazonaws.com/0/65634/43f22d89-fa88-395c-8a56-612167bb9ae0.png">

Note: This article is an English translated version of this [article](http://qiita.com/codelynx/items/899c26dd2cbdba7d2b00).

## for var i = 0 ; i < 100 ; i++

This scenario is the most typical `for` statement from 0 to 99.

```.swift
// C-Style for statement
for var i = 0 ; i < 100 ; i++ {
	print("\(i)")
}
```

```.swift
// Swift 3.0 ready
(0 ..< 100).forEach { print("\($0)") }
```

```.swift
// Swift 3.0 ready
for i in (0 ..< 100) {
	print("\(i)")
}
```

## for var i = 99 ; i >= 0 ; i--

Typical downward `for` statement from 99 to 0.

```.swift
// C-Style for statement
for var i = 99 ; i >= 0 ; i-- {
	print("\(i)")
}
```

You ay use `reverse()`.

```.swift
// Swift 3.0 ready
(0 ..< 100).reverse().forEach { print("\($0)") }
```

```.swift
// Swift 3.0 ready
for i in (0 ..< 100).reverse() {
	print("\(i)")
}
```

## for var i = 0 ; i < 100 ; i += 2

Case where it's incrementation is more then one.


```.swift
// C-Style for statement
for var i = 0; i < 100 ; i += 2 {
	print("\(i)")
}
```

You may use `stride()`. It has a parameter not only `to` but also `through`.

```.swift
// Swift 3.0 ready
0.stride(to: 100, by: 2).forEach { print("\($0)") }
```

> #### Difference between `stride(to:by:)` an `stride(through:by:)`
>
> Note that `to` refers to "less than", and `through` refers to "equal or less than".  Please see these examples.
>
>```.swift
>0.stride(to: 20, by: 5) // 0, 5, 10, 15
>```
>
>```.swift
>0.stride(through: 20, by: 5) // 0, 5, 10, 15, 20
>```

## for var i = 98 ; i >= 0 ; i -= 2

Case when decrementing.

```.swift
// C-Style for statement
for var i = 98 ; i >= 0 ; i -= 2 {
	print("\(i)")
}
```

```.swift
// Swift 3.0 ready
98.stride(through: 0, by: -2).forEach { print("\($0)") }
```

```.swift
// Swift 3.0 ready
0.stride(to: 100, by: 2).reverse().forEach { print("\($0)") }
```

```.swift
// Swift 3.0 ready
for i in 0.stride(to: 100, by: 2).reverse() {
	print("\(i)")
}
```

## When incrementation or decrementation is given in body code, and it's varying depending on condition.

I cannot come up with good example, but say, incrementation is depending on it's squared number is whether odd or even. (This example is totally a fictional)

```.swift
// C-Style for statement
for var i = 0 ; i < 100 ; {
	print("\(i)")
	if (i * i) % 2 == 0 { i += 1 }
	else { i += 2 }
}
```

You may rewrite this with `while` statement.

```.swift
// Swift 3.0 ready
var i = 0
while i < 100 {
	print("\(i)")
	if (i * i) % 2 == 0 { i += 1 }
	else { i += 2 }
}
```

## When there are some cases to leave `for` statement.

I cannot come up with good example once again, but say, there is a case when you generate a random number and leave `for` statement depending on the generated number.

```.swift
// C-Style for statement
for var i = 0 ; i < 100 ; i++ {
	print("\(i)")
	if arc4random_uniform(10) == 0 { break }
}
```

You may not use `forEach {}` to break `for` statement, so you may use `for`-`in` statement.

```.swift
// error: forEach で break は使えない
(0 ..< 100).forEach {
	print("\(i)")
	if arc4random_uniform(10) == 0 { break } // error
}
```

```.swift
// Swift 3.0 ready
for i in 0 ..< 100 {
	print("\(i)")
	if arc4random_uniform(10) == 0 { break } // OK
}
```

If your code has `continue` statement, you may not use `forEach` statement, so as `break` statement.  So please rewrite that code with `for`-`in` statement as well.

## Floating number, Not Integer

You may use `stride()` for `CGFloat` and `Double` because they are `Strideable` as well.

```.swift
CGFloat(0).stride(to: 100, by: 1.5).forEach { print("\($0)") }
```

```.swift
0.0.stride(to: 100.0, by: 1.5).forEach { print("\($0)") }
```

## The last word 

When Apple announces Swift 3.0, Apple may state something like "We provide best converter you can count on."  Well, I am sorry, I don't think I would trust.  Instead, Apple gave us some time to rewrite part of our code bit by bit, and this is much better way to be part of the Swift revolution.

Then Swift programmers!! Have a happy coding!!

```.console
Swift Version:
Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29)
```