如何在Swift中的for循环中跳过索引 [英] How to skip an index in a for loop in Swift
问题描述
我正在尝试在随机条件下跳过,如果为true,则将跳过循环,但结果是打印所有元素0,1,2,3,4
我知道在Java中如果索引增加,索引将跳过,但是Swift不会发生这种情况.
更新:这是我编写的某些程序的简化版本,print()
必须在每个循环后立即发生,并且索引仅在某些未知条件下才递增,我希望它的行为类似于JAVA.
for var index in 0..<5 {
print(index)//this prints 0,1,2,3,4, this must happen right after for loop
if some unknown condition {
index+=1
}
}
索引在循环中自动递增,您可以使用where子句跳过索引:
for index in 0..<5 where index != 2 {
print(index)
}
I'm trying to skip in a random condition that if true, will skip a loop, but results are printing all elements 0,1,2,3,4
I know in Java if index is incremented index will skip, but this is not happening Swift.?
Update: this is a simplified version of some program I wrote, the print()
has to be happen right after each loop, and index is ONLY incremented in some unknown conditions, I want it to behave like JAVA.
for var index in 0..<5 {
print(index)//this prints 0,1,2,3,4, this must happen right after for loop
if some unknown condition {
index+=1
}
}
The index is automatically incremented in the loop, you can skip an index with a where clause:
for index in 0..<5 where index != 2 {
print(index)
}
这篇关于如何在Swift中的for循环中跳过索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!