NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: A newbie question about conditions >, < or ..<, ...

Forums > Swift


dunno what's the difference or the point of ..< and ... when <, >, <= or >= are already there to be the range operator.. re them the similiar ?


>, <, >=, and <= aren't range operators, though; they are comparison operators. So to use them to test for inclusion in a range requires multiple comparisons in combination with logical operators.

Which of these examples looks and reads better?

//using the ~= operator
if 0..<10 ~= i {

//using the Range.contains() method
if (0..<10).contains(i) {

if i >= 0 && i < 10 {

Its also far, far easier to loop through a range with something like this:

for i in 0...10 {

than this:

i = 0
while i >= 0 && i <= 10 {
    i += 1

Especially because when using comparison operators like in that last example, it is very easy to mess up and use, say, < 10 when you mean to say <= 10.



Thanks a lot such a high quality reply dude. I get it so they re not used mostly in range but to compare both values, it is bit more convenient and simple to use ... with less combination operator as well.


Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.