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

谁说我不能喝 提交于 2019-12-01 12:33:07

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
    }
}

The easiest way is using continue within the if condition

       for index in 1...100
       {
            if index == 5
            {
               continue
            }
        print(index)//1 2 3 4 6 7 8 9 10
        }

Or

for index in 1...10 where index%2 == 0
{
  print(index)//2 4 6 8 10
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!