-4

I am trying to make a simple if and and and condition but it breaks the operator and gives the same result: Your are a parrot. Is there any fix?

                if (mouse > dog & mouse > cat & mouse > parrot);
                {
                    this.BackgroundImage = Animal_Personality_Test.Properties.Resources.a;
                }
                if (dog > mouse & dog > cat & dog > parrot) ;
                {
                    this.BackgroundImage = Animal_Personality_Test.Properties.Resources.b;
                }
                if (cat > mouse & cat > dog & cat > parrot) ;
                {
                    this.BackgroundImage = Animal_Personality_Test.Properties.Resources.c;
                }
                if (parrot > mouse & parrot > dog & parrot > cat) ;
                {
                    this.BackgroundImage = Animal_Personality_Test.Properties.Resources.d;
                }

I was expecting: You are a mouse!

                if (mouse > dog & mouse > cat & mouse > parrot);
                {
                    this.BackgroundImage = Animal_Personality_Test.Properties.Resources.a;
                }
                if (dog > mouse & dog > cat & dog > parrot) ;
                {
                    this.BackgroundImage = Animal_Personality_Test.Properties.Resources.b;
                }
                if (cat > mouse & cat > dog & cat > parrot) ;
                {
                    this.BackgroundImage = Animal_Personality_Test.Properties.Resources.c;
                }
                if (parrot > mouse & parrot > dog & parrot > cat) ;
                {
                    this.BackgroundImage = Animal_Personality_Test.Properties.Resources.d;
                }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Puro
  • 1
  • 4
  • 1
    Maybe you meant `&&` (logical and) instead of `&` (binary and)? Also why did you write `;` after each `if` line? Those should definitely not be there – Rafalon Nov 26 '22 at 07:18
  • 1
    @Rafalon please do not misguide people - both `&&` and `&` are logical operators when acting on boolean values. I assume you know the difference in behavior between those too and just decided to mis-name one. – Alexei Levenkov Nov 26 '22 at 07:41
  • @AlexeiLevenkov, by the time I realized the names were wrong (apparently it should be conditional vs logical and not logical versus binary), I couldn't edit my comment anymore. Thanks for pointing that out. One can see in the linked question that this naming confusion is quite spread – Rafalon Nov 26 '22 at 07:44

1 Answers1

3

In C#, writing:

if (condition) ; // <-- note the ; here
{
  foo();
}

is equivalent to:

if (condition) { /* do nothing */ }
foo(); // <-- do foo() anyway

Therefore, your current code will just sequentially replace the background:

this.BackgroundImage = Animal_Personality_Test.Properties.Resources.a;
this.BackgroundImage = Animal_Personality_Test.Properties.Resources.b;
this.BackgroundImage = Animal_Personality_Test.Properties.Resources.c;
this.BackgroundImage = Animal_Personality_Test.Properties.Resources.d;

Which is why you get the last image in the end, regardless your conditions.

You should definitely get rid of those ; at the end of your if lines.


As a side note, you should be aware that & and && are different things. See c# - Usage of & versus &&

Rafalon
  • 4,450
  • 2
  • 16
  • 30