Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Control Flow If Statement

What type are the numbers in a range (1...13)

when you have a statement like

let cards = 1...13

you are including the range 1 to <14. Does this mean that 13.999999 is included? Are the numbers in the range double or float or are they int?

1 Answer

kirkbyo
kirkbyo
15,791 Points

When you are creating a range it is of type int. I tried playing around with it in a playground and I don't think it is possible to create a range of type float or double. So when creating a range of 1 to 5, you are only getting the whole numbers. If you want to try this out in a playground you can use a for-loop.

let cards = 0...33
for items in cards {
    print("\(items) \n")
}

Hope this helped,

Ozzie

Thanks Ozzie. I just saw the

 "0..<14" 

in the standard editor output area and thought that it might include everything up to 14 but I suppose it's just an easy way to write "whole numbers up to 14."

I tried running your code and it spits out whole numbers. I tried putting Doubles in to the interval and it doesn't like it.

kirkbyo
kirkbyo
15,791 Points

Is there a certain reason why you would like to use a float instead of an int?

Nope, just wanted to see if it was possible because I saw the less than sign. Initially I was trying to see if a double like 5.9 was in the recognized range...not necessarily to make the range go from 3.9...7.9.

I tried this

let tests = [1, 2.3, 3, 5]

for var i = 0; i < tests.count; i++ {
    switch tests {
    case 1...3:
        println("We can include doubles")
    default:
        println(tests)
    }
}

And based on the error it returns I don't think it is possible for a range to even contain a Double.