【问题标题】:C# build the if conditions at runtimeC# 在运行时构建 if 条件
【发布时间】:2013-05-31 16:13:12
【问题描述】:

我已经嵌套了 if else 结构,或多或少做同样的事情。

只是一个 if(A && B && C) 但我分别有条件 A 和 C 的标志 D 和 E。

这意味着如果 D 为假,A 应该消失并且不被评估。如果 E 为假,则 C 不被评估也是如此。

现在,我的代码如下所示:

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}
  }
}

有没有什么简单的方法可以将这个复杂的结构简化为几行代码?

【问题讨论】:

  • 为什么要减少到几行?阅读代码可能更复杂。据我了解,编写代码是为人类理解代码而设计的。编译器会将其编译为机器代码,而现代编译器无论如何都会优化代码......
  • 你不能在第一种情况下结合D , A , B and E 来评估单个if 吗?只有当所有人都是true时,你才检查C
  • 在 if 语句的两个分支中 Do Somethings 和 Do Something elses 是否相同?
  • Oren:是的,“做某事”和“做其他事情”在两个分支中都在做同样的事情。
  • 有一个小问题...改进答案

标签: c# optimization if-statement nested-statement


【解决方案1】:

由于两个分支操作相同,您基本上可以编写:

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

希望您的变量比 A、B、C 等更具可读性:)

【讨论】:

  • 奥伦,很好用!是的,您确实在语句中捕捉到了正确的条件!
【解决方案2】:
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");
                }

        }

    }

【讨论】:

  • 如果将 D 和 A 与 or 结合使用将不起作用,因为当 D 为真时 A 将消失且不被评估。如果使用 ||像这样,当 D 为真且 A 没有像往常一样评估时,第二个条件将始终返回真!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2018-01-11
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
相关资源
最近更新 更多