-1

how do I get the compiler to check both the left and right side of the statement? if I'm not mistaken, i think in C language, it reads both left and right if you have && or || .... so when I looked this up for C++, it says only checks if the left is true....what I need is to be able to check if both sides are true.

so:

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

this issue I'm having is, it reads the left side only, and so transactions only returns 1.

Thank you for your time!

EDIT

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

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

SorryEh
  • 900
  • 2
  • 15
  • 47
  • 3
    I think C also supports short-circuit evaluation, the same way that C++ does. http://en.wikipedia.org/wiki/Short-circuit_evaluation – Chris Feb 24 '15 at 21:03
  • 3
    It's not that they just evaluate one side, they evaluate the left statement first, and if the result can be determined, they don't evaluate the right statement. For example, `T || anything` is always true, so if the left hand of a `||` operator is true, there is no need to evaluate the right hand of that operator. Same for `F && anything`. However, if you have, for example, `T && something` the result is not known until you evaluate the right hand side. – triple_r Feb 24 '15 at 21:05
  • @triple_r that makes perfect sense. I didn't see it that way at all. thank you – SorryEh Feb 24 '15 at 21:06

5 Answers5

8

Put the && conditional first and then the || conditional as the else if.

An explanation, courtesy of zenith (+1 him in the comments if this helps you):

The most restrictive case needs to go first, since if A && B is true, A || B will always be true anyway. And thus if you put && after ||, the || case will catch whenever the && case is true.

Also, another side note: leave the cout outside of all the brackets and you can remove the else. It's getting printed no matter what so no need to type it 3 times.

Foggzie
  • 9,691
  • 1
  • 31
  • 48
  • i swapped the && and || and its same situation, this time returning 2 instead of 1 when trying the other condition :/ i have to be doing something wrong. – SorryEh Feb 24 '15 at 21:04
  • 1
    @Umeed Did you swap just the && and || or did you move the +1 and +2 with them? The +2 should still be under the &&. – Foggzie Feb 24 '15 at 21:07
  • when i put 0 on deposit, and 1 on withdraw, it still returns 2 (same happens when i do the other way around). – SorryEh Feb 24 '15 at 21:12
  • 2
    @GuntherFox A few words on *why* `&&` needs to go before `||` would be good. – Emil Laine Feb 24 '15 at 21:16
  • I may have figured out why its not working. It seems that whatever my balance remaining is, is what decides what's being printed. Thanks so much for the help. I think i got this now. – SorryEh Feb 24 '15 at 21:19
  • 2
    Yeah the *most restrictive* case needs to go first, since if `A && B` is true, `A || B` will always be true anyway. And thus if you put `&&` after `||`, the `||` case will catch whenever the `&&` case is true. – Emil Laine Feb 24 '15 at 21:25
  • I can't seem to figure out why my "initial balance" is affecting the outcome. I posted the rest of my code if anyone can take a look. – SorryEh Feb 24 '15 at 21:56
  • @zenith Thanks! Added a shout out. – Foggzie Feb 24 '15 at 22:11
  • @GuntherFox if you have a moment, can you take a look at the remaining code i posted up through the ideone links. It seems my initial and remaining balance are affecting my if statement's return value. I don't understand why (or do i need to post this as a new question?) – SorryEh Feb 24 '15 at 22:18
  • 1
    @Umeed Yeah this might get a little too cluttered (and the current question is not what your new question is about so), just make an [MCVE](http://stackoverflow.com/help/mcve) and post a new question. – Emil Laine Feb 24 '15 at 22:21
  • @Umeed It's time for a new question. – Foggzie Feb 24 '15 at 22:38
4

You're not right about C. The || "logical or" operator terminates as soon as one side is true, and it starts to evaluate left to right.

However, that's irrelevant here. Use De Morgan's law to transform your || to (not) and where possible.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
3

You could rewrite the if statements the following way

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

The other approach is to use the following expression

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

Or simply

 int condition = ( deposit >= 1 ) + ( withdraw >=1 )

 transactions = transactions + condition;
 cout << "Transactions:  " << transactions << endl;

Or

 int condition = ( deposit >= 1 ) + ( withdraw >=1 )

 transactions += condition;
 cout << "Transactions:  " << transactions << endl;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • would these approaches work better than what I have? if so, why? – SorryEh Feb 24 '15 at 21:20
  • 1
    @Umeed You code is invalid because if the both conditions are true the second else if will not be executed. As for the shortened code shown in my post then it just simpler.:) – Vlad from Moscow Feb 24 '15 at 21:21
  • I've tried your second approach there with `int condition` and it seems to sort of work. if deposit is 0 and withdraw 1, i get a return of 1....but if deposit is 1 and withdraw is 0 i get a return of 2. I'm pretty sure the issue is being affected by something else. I will keep working on this, thank you very much. – SorryEh Feb 24 '15 at 21:25
  • 1
    @Umeed If the both conditions are true then this statement int condition = ( deposit >= 1 ) + ( withdraw >=1 ) is equivalent to int condition = true + true; and equal to 2. If one of the conditions is true and the other is false then the result is equal to 1. Otherwise the result is equal to 0 because false + false == 0. – Vlad from Moscow Feb 24 '15 at 21:28
  • Hi Vlad, I can't seem to figure out why my "initial balance" is affecting the outcome. If you don't mind and have some time to spare, do you mind taking a look? the "balance" variable (main.cpp) is affected by the DepositAmt and WithdrawAmt functions found in account.cpp. I don't see why they would affect my if statements. – SorryEh Feb 24 '15 at 21:58
  • @Umeed I am sorry. I think if there is a more problem with the code you may ask a new question. – Vlad from Moscow Feb 25 '15 at 06:01
2

Since both requirement 1 && 2 can evaluate to true, you should take the nested if/else selection statement out of the code. Unfortunately, vlad’s piece of elegant code above will not accurately fulfill the requirements. Since requirement 1 and 2 can both evaluate to true, transactions should have the ability to equal 3.

The code below accurately fulfills your stated requirements.

if (deposit >=1 || withdraw >=1)
    ++transactions;

if (deposit >=1 && withdraw >=1)
    transactions += 2;

if (deposit < 1 && withdraw < 1)
    transactions = 0;

cout << "transactions: " << transactions;
0

I know this is old, but I want to give my two cents here and really just give people who read this the easiest solution.

operator || will immediately resolve to true and jump in if the first condition it evaluates is true, who cares which one it is?

operator && will only resolve to true and step in when both conditions are evaluated as being true, who cares which one it reads first?

But to answer, C compilers will evaluate the left expression first and short-circuit with operator ||,operator && and operator ,.

It's more logical in my view, and more readable, to evaluate the Boolean expressions separately:

    if(deposit >= 1) {
        transactions += 1;
    }
    if(withdraw >= 1) {
        transactions += 1;
    }

If you wanted to shorthand this, you could do,

int transactions = ((withdraw >= 1) ? 1 : 0) + ((deposit >= 1) ? 1: 0);

but I find that harder to read and sometimes you'll do 0+0, not sure the (in)significance of that.

Because you want to evaluate two different functional requirements that I assume produce different outcomes, keep them separated. Your third functional requirement is already evaluated before you even think about the first two as you've initialised to zero. Just my opinion.

The int condition = ... route is also okay but you're explicitly reserving memory for literally no reason. Not that it particularly matters these days, but C is all about that lower level headache. Plus, my second example does what an earlier comment did without the extra evaluations.

You've taken the if-else route, which is fine when done properly, but you're trying to evaluate a logic problem when it's true, not when it's false, and else will only happen when your condition fails. Forget about trying to gotcha the compiler and think about the logic you're trying to implement, because 9.9999999 times out of 10 it sure ain't the compiler's problem. In fact, the compiler did its job by evaluating what you delegated to it.