[swift-users] For loop question?

Erica Sadun erica at ericasadun.com
Tue Dec 15 07:46:54 CST 2015


Let me add that stride offers two variations: "to" and "through", which you can see here: 
http://swiftstub.com/102043338

The to variation documentation states:

    /// Return the sequence of values (`self`, `self + stride`, `self +
    /// stride + stride`, ... *last*) where *last* is the last value in
    /// the progression that is less than `end`.

The through variation:

    /// Return the sequence of values (`start`, `start + stride`, `start +
    /// stride + stride`, ... *last*) where *last* is the last value in
    /// the progression less than or equal to `end`.
    ///
    /// - Note: There is no guarantee that `end` is an element of the sequence.

-- E


> On Dec 15, 2015, at 1:14 AM, Jacob Bandes-Storch via swift-users <swift-users at swift.org> wrote:
> 
> You can use `stride`:
> 
> for i in 0.stride(to: 10, by: 2) {
>     print("i is \(i)")
> }
> 
> On Tue, Dec 15, 2015 at 12:08 AM, Eirny Kwon via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> for loop is working
> 
> for var i = 0; i < 10; i += 2 {
>     print("i is \(i)")
> }
> 
> 
> On December 15, 2015 at 5:05:41 PM, cooper liu via swift-users (swift-users at swift.org <mailto:swift-users at swift.org>) wrote:
>> Hi,
>> If the c style for loop was removed,how to implement the following code beautifully:
>> For(var i=0; i < 10; i+=2)
>> Since the range operator ... doesn't support step other than 1, and it doesn't support end value < start value.
>> 
>> Thanks!
>> 
>> 
>>  _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> 
>  _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151215/9f11defe/attachment.html>


More information about the swift-users mailing list