NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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 Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.