[swift-evolution] C-style For Loops

David Waite david at alkaline-solutions.com
Mon Dec 7 18:10:53 CST 2015


> On Dec 7, 2015, at 4:58 PM, Ray Fix via swift-evolution <swift-evolution at swift.org> wrote:
> There is an improvement in that it forced us to avoid integer overflow by specifying the end game (although the heat death of the universe might happen first if you run this in a playground).  Since j escapes the scope of it’s loop we couldn’t use a for statement variable.  I think this is actually a good thing because it makes this wrinkle more apparent.
> 
You can use a for loop, you just have to use a flag to indicate whether the number was found to be non-prime, or a label. Label approach (since using a flag is obvious);

candidate: for i in 2 ..< Int.max {
    for j in 2 ..< i {
        if i % j == 0 {
           continue candidate
        }
    }
    print(i)
}

-DW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151207/3387f4e4/attachment.html>


More information about the swift-evolution mailing list