0

I want to do IF under serveral condition AND && and OR || on it

i have two range of number Top1 - Bottom1 and Top2 - Bottom2

and only executed when Currentnumber is within these two range am i doing it okay ?

if (OnlyAbove == True
    && (Top1 > Currentnumber || Top2 > Currentnumber)
    && (Bottom1 < Currentnumber || Bottom2 < Currentnumber)
   )
{
   Statement
}
  • 2
    Possible duplicate of [C# conditional AND (&&) OR (||) precedence](https://stackoverflow.com/questions/1196703/c-sharp-conditional-and-or-precedence) – Broots Waymb Apr 01 '19 at 20:08
  • 2
    have you tested it? You should really create unit tests for all the cases – bolov Apr 01 '19 at 20:10
  • also should the number be within both of these ranges (aka within the intersection) or within any of those ranges (aka union)? – bolov Apr 01 '19 at 20:12
  • If a number is between Bottom1 and Top1, then it must be greater (or greater than or equal to, depending on your rules) Bottom1 _**AND**_ it must be less than (or <=) Top1. And, if it can be either in one range (using the rules above) _**OR**_ in the other range (using similar rules, but different boundaries). Write your expressions accordingly. Use parentheses to make it clear to the reader what you really mean. – Flydog57 Apr 01 '19 at 20:15
  • Do you have a member called `True` with capital T? That would be confusing. If you mean the keyword `true`, do not compare a non-nullable Boolean expression to the `true` literal. Instead use `if (OnlyAbove && …)`. – Jeppe Stig Nielsen Apr 01 '19 at 20:22
  • @Flydog57 i made this && (((Top1 > Currentnumber) && (Bottom1 < Currentnumber)) || (Top2 > Currentnumber) && (Bottom2 < Currentnumber))) – Alif Novaldi Apr 01 '19 at 20:58

1 Answers1

2

I'm going to go out on a limb and say: no, you're not doing right on this one - mostly because what your code is currently doing doesn't make sense.

One thing that helps out with stuff like this is to phrase it in common english - so you go from:

if (OnlyAbove == True
    && (Top1 > Currentnumber || Top2 > Currentnumber)
    && (Bottom1 < Currentnumber || Bottom2 < Currentnumber)
   )
{
   Statement
}

... to ...

if OnlyAbove
    AND (CurrentNumber above at least one of the two Tops)
    AND (CurrentNumber below at least one of the bottoms)

After all, the stuff between those inner parenthesis is saying "Top1 > current OR Top2 > current" - which really isn't what you're looking for.

Something that'll help out - not only writing code like this, but reading it down the line, is to go a bit overboard with variables. Variables are awesome at helping you document logic, since you can name them very descriptively.

So imagine code that looks like this:

bool betweenRange1 = (Currentnumber > Bottom1) && (Currentnumber < Top1);
bool betweenRange2 = (Currentnumber > Bottom2) && (Currentnumber < Top2);
if (OnlyAbove && betweenRange1 && betweenRange2)
{
    Statement
}

... that IF statement starts to look a lot more readable - it's obvious at a glance at what it's getting at. If you're having trouble parsing IF statements, it's not a bad habit to get into. Maybe a bit overkill... but overkill's a lot better than having code you're not sure of what it's doing :-)

Kevin
  • 2,133
  • 1
  • 9
  • 21