ppalancica - 7 months ago 55

Swift Question

I am trying to do convert this C code:

int i;

`for (i = 9; i >= 0; i--) {`

}

in Swift 3, but I am not sure how to do it.

I know that if if I want to do it in ascending order if i, I can simply write:

`for i in 0..<10 {`

}

But how do I do it in descending order of i?

Thanks in advance!

Answer

Two ways:

```
// reverse a range
for i in (0...9).reversed() {
// ...
}
// use stride
for i in stride(from: 9, through: 0, by: -1) {
// ...
}
```

Source (Stackoverflow)