zainab zainab - 1 month ago 5
Swift Question

I have error in swift loop

am new in swift I tried to make loop like this

for var z = 0 ; z <= 25 ; z = z + 5 {

print (z)
}


put it gives me this error


Loop.playground:5:1: error: C-style for statement has been removed in Swift 3


how can I solve it?

Answer

You can use the where clause!

for z in 0...25 where z % 5 == 0 {
    print(z)
}

It basically filters out z values and it only leaves the z values that are divisible by 5, which is what you want.

This is wayyyy more swifty than the C-style for loop.

Another way to do this is with stride:

for i in stride(from: 0, through: 25, by: 5) {
    print(i)
}