Haroon
Haroon

Reputation: 3472

c# understanding the bool expression

This is a really simple question I am sure, but I cannot figure out why this assertion fails...

basically if IsApple is false or IsBannana is false assertion should fail, however if one of the two is true assertion should pass, could anyone explain why this assertain fails?

        [Test]
        public void IsApplesOrBannans()
        {
            bool IsApple = true;
            bool IsBannana = false;

            if (!IsApple || !IsBannana)
                Assert.Fail();

            Assert.Pass();
        }

Upvotes: 2

Views: 364

Answers (9)

fixagon
fixagon

Reputation: 5566

(!A|| !A) = !!(!A || !B) = !(A && B) = !(true && false) --> false

Upvotes: 0

Botz3000
Botz3000

Reputation: 39650

Your pass condition contradicts your fail condition, because if only one is true, the other one is false and thus fulfills the fail condition.

if you want it to make sense, change it to this:

if (!IsApple && !IsBanana)
    Assert.Fail():

or, if you also don't want it to be an apple and banana at the same time

if (IsApple == IsBanana)
    Assert.Fail():

Upvotes: 0

CodesInChaos
CodesInChaos

Reputation: 108840

Your pass condition is IsApple || IsBannana, so your fail condition can be written as:

if (!(IsApple || IsBannana))

Or alternatively you can say both must be false to fail:

if ((IsApple==false) && (IsBannana==false))

Which usually is written as:

if (!IsApple && !IsBannana))

Personally I think the first of these three alternatives is the best. But that's just a stylistic choice. functionally they are equivalent.

Or even better, you could switch your then and else part of the if:

public void IsApplesOrBannans()
{
    bool IsApple = true;
    bool IsBannana = false;

    if (IsApple || IsBannana)
        Assert.Pass();
    else
        Assert.Fail();            
}

Or even throw out the if statement entirely:

public void IsApplesOrBannans()
{
    bool IsApple = true;
    bool IsBannana = false;

    Assert.IsTrue(IsApple || IsBannana);
}

Upvotes: 0

Jackson Pope
Jackson Pope

Reputation: 14660

The assert will fail if EITHER is false, it will only pass if BOTH are true.

Edit: I would re-write this as:

if (IsApple || IsBanana)
{
    Assert.Pass();
}
else
{
    Assert.Fail();
}

Interestingly, the question as posed cannot be answered (since if one is true and the other is false according to the question the expected result is amiguous).

Upvotes: 4

faester
faester

Reputation: 15086

You dont want the case where neither a nor b is false, or rather at least one should be true, so

   [Test]      
    public void IsApplesOrBannans() 
    {      
       bool IsApple = true;     
       bool IsBannana = false;    
       if (!(IsApple || IsBannana))   
            Assert.Fail();        
       Assert.Pass();       
    }

Upvotes: 0

sehe
sehe

Reputation: 393674

        if (IsApple != IsBannana)
            Assert.Fail();

I think this trick != is a C# FAQ as poor man's XOR

Upvotes: 0

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248199

What you're saying makes no sense.

Here's how I (and the compiler) understand what you're saying:

basically if IsApple is false or IsBannana is false assertion should fail

  • If IsApple is false the assertion should fail
  • If IsBanana is false, the assertion should fail

In other words, if one of them is false, you don't care whether or not the other is also false.

however if one of the two is true assertion should pass

  • If one of them is true, you don't care whether the other one is also true.

Those requirements contradict each others.

Perhaps you meant "if IsApple is false AND IsBanana is false"; that is, if they are both false.

But what you wrote was "if IsApple is false OR IsBanana is false", that is, if one of them are false.

Upvotes: 5

Andreas Eriksson
Andreas Eriksson

Reputation: 9027

Do this instead:

if(!IsApple && !IsBannana)

Upvotes: 1

Emond
Emond

Reputation: 50692

!IsBannana is true so the if evaluates to true.

I bet you wanted:

if(!IsApple && !IsBananna)
    Assert.Fail();

Upvotes: 4

Related Questions