taylor swift taylor swift - 16 days ago 5
Swift Question

How to determine if Swift loop was interrupted

In python we can use

for a in A:
...
else:
...


and have the
else
statement execute if the loop finished normally. Is there a similar construct in Swift?

Answer

Such a feature was discussed on the Swift Evolution mailing list and – as far as I can see – rejected. See

which also shows a possible workaround:

outer: do {
    for i in 0..<10 {
        if i == 5 { break outer }
    }
    print("no break occurred")
}
Comments