1

I have nested if else structure, more or less doing the same thing.

simply it's a single if(A && B && C) but I have flag D and E for condition A and C respectively.

That means if D is false, A should be disappear and not evaluated. It's same for C not be evaluated if E if false.

Now, my code now is similar as follows:

if (D){
 if (A && B){
    if (E){
       if (C)
         { Do Something }
    } else { Do Something else}
  }
} else
  if (B) {
     if (E){
       if (C)
         { Do Something }
    } else { Do Something else}
  }
}

Is any easy way to reduce this complicated structure to several lines of code?

Donald
  • 33
  • 6
  • Why you want to reduce to few lines? It may be more complicated for reading code. As I understand, writing code are designed for humans to understand code. Compiler will compile it to machine code and modern compiler optimize the code anyway.... – Fabio Jun 05 '13 at 05:02
  • Can't you combine `D , A , B and E` in the first case to evaluate in a single `if` ? you are checking `C` only if all are `true` – V4Vendetta Jun 05 '13 at 05:02
  • are both `Do Something`s and `Do Something else`s the same in both branches of the if statement? – Oren Jun 05 '13 at 05:09
  • Oren: Yes "Do Something" and "Do Something else" is doing the same thing in the both branches. – Donald Jun 05 '13 at 05:15
  • there was a small issue...improving the answer – Jack Jun 05 '13 at 05:34
  • equivalent of (A&&B&&C) considering flags is if( B && (!D || A) && (!E || C)) please see the code and test function below – Jack Jun 05 '13 at 05:58
  • Improve your algorithm, this will reduce the complexity of your code. – TT. Jun 05 '13 at 15:05

2 Answers2

1

Since both branch actions are the same you could essentially write:

        if ((D && A && B) || (!D && B))
        {
            if (E && C)
            {
                DoSomething();
            }
            else
            {
                DoSomethingElse();
            }
        }

Hopefully your variables are more readable than A,B,C etc :)

Oren
  • 3,273
  • 2
  • 19
  • 15
0
I have tested it in c since I am on unix console right now. However, logical operators work the same way for c#. following code can also be used to test the equivalance.

        #include<stdio.h>
        void test(int,int,int,int,int);
        void test1(int,int,int,int,int);
        int main()
        {
            for(int i =0 ; i < 2 ; i++)
              for(int j =0 ; j < 2 ; j++)
                 for(int k =0 ; k < 2 ; k++)
                    for(int l =0 ; l < 2 ; l++)
                       for(int m =0 ; m < 2 ; m++)
                       {
                          printf("A=%d,B=%d,C=%d,D=%d,E=%d",i,j,k,l,m);
                          test(i,j,k,l,m);
                          test1(i,j,k,l,m);
                           printf("\n");

                       }
             return 0;
        }


        void test1(int A, int B, int C, int D, int E)
        {
          if( B && (!D || A) && (!E || C))
            {
                printf("\n\ttrue considering flags");
            }
            else
            {
            printf("\n\tfalse considering flags");
            }
        }
        void test(int A, int B, int C, int D, int E)
        {
        if(D)
        {
            if( A && B)
              if(E)
                 if(C)
                {
                    printf("\n\ttrue considering flags");
                }
                else
                {
                    printf("\n\tAB !C  DE");
                }
        }
        else
        {
            if(  B)
              if(E)
                 if(C)
                {
                    printf("\n\t!D --ignore A-- BC E");
                }
                else
                {
                    printf("\n\tfalse  considering flags");
                }

        }

    }
Jack
  • 741
  • 1
  • 8
  • 25
  • it doesn't work if combining D and A with or because A will disappear and not be evaluated when D is true. If using || like this, 2nd condition will always return true when D is true and A is not evaluated as usual! – Donald Jun 05 '13 at 05:30