2

i have to modify my for loops of my swift 2 app. at the moment i use this syntax

for (var x = 0; x < 5; x++) {

i learned that i have to use this:

for x in 0..<5 {

but how i have to change this for loop:

for (var x = 0; x < 6; x = x+2) {
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Stack108
  • 915
  • 2
  • 14
  • 35
  • Generally – as advised in your former question – **do not** use parentheses around `if / for / while` etc. conditions in Swift – vadian Mar 23 '16 at 10:16

2 Answers2

6

use stride function

 // for x<6
 for i in 0.stride(to: 6, by: 2) {
    print(i)   // 0,2,4
 }

 //for x<=6
 for i in 0.stride(through: 6, by: 2) {
    print(i)  // 0,2,4,6
 }
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
1

In Easy way Try this,

var x = 0
for x in 0..<5 {
    x += 2
}
print(x)

check this link for more reference Swift 2.2 tour

Badal Shah
  • 7,541
  • 2
  • 30
  • 65