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

Aha, so you mean I should have only ifs and not if-elseif. Good, I will try that!

Replies are listed 'Best First'.
Re^5: IF condition with a range (updated)
by haukex (Archbishop) on Jul 17, 2018 at 08:59 UTC
    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.