c ++如果语句条件,则左右读取

Sor*_*yEh -1 c++ if-statement operand

如何让编译器检查语句的左侧和右侧?如果我没有弄错的话,我认为在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;
        }
Run Code Online (Sandbox Code Playgroud)

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

感谢您的时间!

编辑

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

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

Fog*_*zie 8

&&首先是条件,然后是||条件else if.

一个解释,由天顶提供(如果这有助于你,则在评论中给他+1):

最具限制性的案例需要先行,因为如果A && B是的话 true,A || B总会true如此.因此如果你把 &&||,该||案件将赶上每当&&情况 true.

另外,另一边注意:离开cout所有支架的外部,你可以删除else.无论如何都要打印,所以不需要输入3次.

  • @GuntherFox关于*为什么*`&&`需要在`||`之前就可以了. (2认同)
  • 是的*最严格的*案例需要先行,因为如果'A && B`为真,那么'A || 无论如何,B`永远都是真的.因此,如果你在`||`之后放置`&&`,只要`&&`case为真,`||`case就会被捕获. (2认同)