in reply to Re^2: IF condition with a range
in thread IF condition with a range

You're thinking of something like this:

if ( it is equal ) { do something; } elsif ( it is equal or close ) { do something else; }

But you want:

if ( it is equal ) { do something; } if ( it is equal or close ) { do something else; }

Or even:

if ( it is equal or close ) { if ( it is equal ) { do something; } do something else; }

Replies are listed 'Best First'.
Re^4: IF condition with a range
by Anonymous Monk on Jul 17, 2018 at 08:44 UTC
    Aha, so you mean I should have only ifs and not if-elseif. Good, I will try that!
      Aha, so you mean I should have only ifs and not if-elseif.

      Note that there is a potential pitfall of if (...) {...} if (...) {...} instead of if (...) { if (...) { ... } ... } - it's easy to make a mistake with the logic:

      if ( /^[brc]at$/ ) { # match "bat", "rat", or "cat" print "It's an animal\n"; if ( /^b/ ) { print "... and it flies!\n"; } }

      This is not the same as:

      if ( /^[brc]at$/ ) { print "It's an animal\n"; } if ( /^b/ ) { print "... and it flies!\n"; }

      Because the latter will match any word that starts with b.

      Of course, in this case, it seems obvious that if the numbers are equal, they will also be within +/- 1 of each other, but mistakes like the above can happen if you don't think the logic through (and test it!).

      Update: For example, consider the special case of Inf that syphilis suggested, which I've now added as a test case to my other post.