【问题标题】:How can I get which part of an if expression is true?如何获得 if 表达式的哪一部分为真?
【发布时间】:2013-08-29 22:47:03
【问题描述】:

假设我有这样的代码:

if(condition1 || condition2 || condition 3 || condition4)
{
// this inner part will be executed if one of the conditions is true.
// Now I want to know by which condition this part is executed.
}

【问题讨论】:

  • 除了打印cout << "condition1=" << coundition1 << " condition2=" << condition2 ... ?
  • @captain:想多解释一下吗?
  • @captain 是什么让你觉得他的if 可以改写为switch。如果他的condition1等实际上是变量,那肯定不行,如果是任意表达式,那也不行。
  • 在执行内部时。编译器知道哪个条件为真。我想知道执行内部的条件是正确的。
  • @mahesh 详细说明你的问题,也许你需要重新设计你的 if 语句

标签: c++ objective-c c if-statement


【解决方案1】:

我确信有更好的方法可以做到这一点,这里有一个:

int i = 0;
auto check = [&i](bool b)->bool
{
    if (!b) ++i;
    return b;
};

if (check(false) || // 0
    check(false) || // 1
    check(true)  || // 2
    check(false))   // 3
{
    std::cout << i; // prints 2
}

【讨论】:

  • 尾随返回类型不是必需的
  • @user1233963 在 C++14 中不会出现,在 C++11 中会出现。
  • @user1233963 尝试使用-pedantic 进行编译。我曾经thought the same 和你一样:)
  • 该警告仅在您以条件返回时出现。单个返回(如本例所示)工作正常
  • 我从来没有说过它不起作用,它只是按照 C++11 规范的格式不正确。
【解决方案2】:

||是短路评估,所以你可以有这样的代码:

if(condition1 || condition2 || condition 3 || condition4)
{
    if (condition1 ) 
    {
            //it must be condition1 which make the overall result true
    }
    else if (condition2)
    {
            //it must be condition2 which make the overall result true
    }
    else if (condition3)
    {
            //it must be condition3 which make the overall result true
    }
    else
    {
            //it must be condition4 which make the overall result true
    }

    // this inner part will executed if one of the condition true. Now I want to know by which condition this part is executed.
}
else
{

}

【讨论】:

    【解决方案3】:

    如果条件相互独立,则需要单独检查,或者,如果它们属于一个变量,则可以使用switch语句

    bool c1;
    bool c2
    if ( c1 || c2 )
    {
        // these need to be checked separately
    }
    
    
    int i; // i should be checked for multiple conditions. Here switch is most appropriate
    switch (i)
    {
        case 0: // stuff
                break;
        case 1: // other stuff
                break;
        default: // default stuff if none of the conditions above is true
    }
    

    【讨论】:

    • 一些if语句不能切换到switch,例如if (str=="something")
    【解决方案4】:

    如果没有switch,您只能使用orif 语句:

    if(condition1 || condition2 || condition 3 || condition4) {
      // this inner part will executed if one of the condition true. 
      //Now I want to know by which condition this part is executed.
      if ( condition1 || condition2 ) { 
        if ( condition1 ) 
           printf("Loop caused by 1");
        else 
           printf("Loop caused by 2");
      else 
        if ( condition3) 
           printf("Loop caused by 3");
        else
           printf("Loop caused by 4");
    }
    

    我不确定这是您见过的最有效的方法,但它会确定四种条件中的哪一种导致进入if ... 块。

    【讨论】:

      【解决方案5】:

      如果您出于编程原因需要知道,即根据哪个条件为真运行不同的代码,您可以这样做

      if (condition1)
      {
          ...
      }
      else if (condition2)
      {
          ...
      }
      else if (condition3)
      {
          ...
      }
      else if (condition4)
      {
          ...
      }
      else
      {
          ...
      }
      

      如果您只是出于调试原因想知道,只需打印输出即可。

      【讨论】:

        【解决方案6】:

        逗号运算符呢? 通过使用遵循短路评估方法的逻辑运算符,以下工作正常:

         int w = 0; /* w <= 0 will mean "no one is true" */
         if ( (w++, cond1) || (w++, cond2) || ... || (w++, condN) )
           printf("The first condition that was true has number: %d.\n", w);
        

        【讨论】:

          猜你喜欢
          • 2016-03-04
          • 1970-01-01
          • 1970-01-01
          • 2013-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多