Search code examples
swiftswift3for-in-loop

How to skip iterations of a for-in loop (Swift 3)


Is it possible to skip iterations of a for-in loop in Swift 3?

I want to do something like this:

for index in 0..<100 {
    if someCondition(index) {
        index = index + 3 //Skip iterations here
    }
}

Solution

  • Simple while loop will do

    var index = 0
    
    while (index < 100) {
        if someCondition(index) {
            index += 3 //Skip 3 iterations here
        } else {
            index += 1
            // anything here will not run if someCondition(index) is true
        }
    }