【问题标题】:c++ Reading both left AND right if statement conditionsc ++读取左右if语句条件
【发布时间】:2015-02-24 21:00:55
【问题描述】:

如何让编译器同时检查语句的左侧和右侧?如果我没记错的话,我认为在 C 语言中,如果您有 &&|| ...,它会同时读取左右两侧。所以当我查找 C++ 时,它说只检查左边是否是真的....我需要的是能够检查双方是否为真。

所以:

//Transactions has been initialized to 0

1. if deposit OR withdraw are greater than or equal to 1, add 1 to variable transactions.
2. if deposit AND withdraw are BOTH greater than or equal 1, then add 2 to variable transactions.
3. else if BOTH are less than 1, transaction is 0.

    if (deposit >= 1 || withdraw >=1)
        {
            transactions = transactions + 1;
            cout << "Transactions:  " << transactions << endl;
        }

    else if (deposit >= 1 && withdraw >=1)
        {
           transactions = transactions + 2;
           cout << "Transactions:  " << transactions << endl;
        }
    else
        {
            cout <<"Transactions: " << transactions << endl;
        }

我遇到的这个问题是,它只读取左侧,因此事务只返回 1。

感谢您的宝贵时间!

编辑

https://ideone.com/S66lXi (account.cpp)

https://ideone.com/NtwW85 (main.cpp)

【问题讨论】:

  • 我认为 C 也支持短路评估,就像 C++ 一样。 en.wikipedia.org/wiki/Short-circuit_evaluation
  • 他们不是只评估一侧,他们先评估左边的语句,如果结果可以确定,他们不评估右边的语句。例如,T || anything 始终为真,因此如果|| 运算符的左手为真,则无需评估该运算符的右手。 F &amp;&amp; anything 也一样。但是,例如,如果您有 T &amp;&amp; something,则在您评估右侧之前,结果是未知的。
  • @triple_r 这很有意义。我根本不这么看。谢谢

标签: c++ if-statement operand


【解决方案1】:

&amp;&amp; 条件放在首位,然后将|| 条件作为else if

解释,由 zenith 提供(如果这对您有帮助,请在 cmets 中为他+1):

最严格的情况需要先处理,因为如果A &amp;&amp; BtrueA || B 将永远是 true。因此,如果你把 &amp;&amp;|| 之后,|| 案例将在 &amp;&amp; 案例出现时捕获 true.

另外,另一个注意事项:将cout 留在所有括号之外,您可以删除else。无论如何都会打印出来,所以不需要输入 3 次。

【讨论】:

  • 我交换了 && 和 ||和它同样的情况,这次在尝试其他条件时返回 2 而不是 1:/ 我必须做错事。
  • @Umeed 你是不是只交换了 && 和 ||还是你和他们一起移动了+1和+2? +2 仍应位于 && 下方。
  • @GuntherFox 关于为什么的几句话 &amp;&amp; 需要在 || 之前进行。
  • 是的,最严格的情况需要先处理,因为如果A &amp;&amp; B 为真,那么A || B 将始终为真。因此,如果您将&amp;&amp; 放在|| 之后,只要&amp;&amp; 为真,|| 就会捕获。
  • @Umeed 是的,这可能有点太混乱了(而且当前的问题不是你的新问题),只需创建一个MCVE 并发布一个新问题。
【解决方案2】:

您对 C 的看法不正确。||“逻辑或”运算符在一侧为真时立即终止,并开始从左到右求值。

但是,这无关紧要。尽可能使用德摩根定律将您的|| 转换为(not) and

【讨论】:

    【解决方案3】:

    你可以通过以下方式重写 if 语句

    if (deposit >= 1 && withdraw >=1)
        {
           transactions = transactions + 2;
           cout << "Transactions:  " << transactions << endl;
        }
    else if (deposit >= 1 || withdraw >=1)
        {
            transactions = transactions + 1;
            cout << "Transactions:  " << transactions << endl;
        }
    
    else
        {
            cout <<"Transactions: " << transactions << endl;
        }
    

    另一种方法是使用以下表达式

    int condition = ( deposit >= 1 ) + ( withdraw >=1 )
    
    if ( condition == 2 )
        {
           transactions = transactions + 2;
           cout << "Transactions:  " << transactions << endl;
        }
    else if ( condition == 1 )
        {
            transactions = transactions + 1;
            cout << "Transactions:  " << transactions << endl;
        }
    
    else
        {
            cout <<"Transactions: " << transactions << endl;
        }
    

    或者干脆

     int condition = ( deposit >= 1 ) + ( withdraw >=1 )
    
     transactions = transactions + condition;
     cout << "Transactions:  " << transactions << endl;
    

    或者

     int condition = ( deposit >= 1 ) + ( withdraw >=1 )
    
     transactions += condition;
     cout << "Transactions:  " << transactions << endl;
    

    【讨论】:

    • 这些方法会比我现有的更好吗?如果是,为什么?
    • @Umeed 您的代码无效,因为如果两个条件都为真,则不会执行第二个 else if。至于我帖子中显示的缩短代码,那就更简单了。:)
    • 我已经用int condition 尝试了你的第二种方法,它似乎有点工作。如果存款为 0 且取款为 1,我将获得 1 的回报……但如果存款为 1 且取款为 0,我将获得 2 的回报。我很确定该问题受到其他因素的影响。我会继续努力的,非常感谢。
    • @Umeed 如果两个条件都为真,那么这个语句 int condition = ( deposit >= 1 ) + (withdraw >=1 ) 等价于 int condition = true + true;并且等于 2。如果其中一个条件为真,另一个为假,则结果等于 1。否则结果等于 0,因为假 + 假 == 0。
    • 嗨弗拉德,我似乎无法弄清楚为什么我的“初始平衡”会影响结果。如果您不介意并且有一些空闲时间,您介意看看吗? “余额”变量 (main.cpp) 受 account.cpp 中的 DepositAmt 和 WithdrawAmt 函数的影响。我不明白为什么它们会影响我的 if 语句。
    【解决方案4】:

    由于要求 1 && 2 都可以评估为真,因此您应该将嵌套的 if/else 选择语句从代码中取出。不幸的是,vlad 上面这段优雅的代码不能准确地满足要求。由于需求 1 和 2 都可以评估为真,因此交易应该能够等于 3。

    下面的代码准确地满足了您声明的要求。

    if (deposit >=1 || withdraw >=1)
        ++transactions;
    
    if (deposit >=1 && withdraw >=1)
        transactions += 2;
    
    if (deposit < 1 && withdraw < 1)
        transactions = 0;
    
    cout << "transactions: " << transactions;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多