For loop question?


(cooper liu) #1

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!


(Eirny Kwon) #2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Jacob Bandes-Storch) #3

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@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Erica Sadun) #4

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@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@swift.org <mailto:swift-users@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@swift.org <mailto:swift-users@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@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users