equals confusion

  • Thread starter Thread starter Matty
  • Start date Start date
M

Matty

Hi,

I'm currently learning c# and am confused why the first example below
doesn't work but the second one does. I'd appreciate any feeback. Thanks.

First example using !=

string input = Console.ReadLine();

if ((input != "0") || (input != "1"))
{
Console.WriteLine("not equal to 0 or 1");
}
else
{
Console.WriteLine("is equal to 0 or 1");
}

Console.ReadLine();

Second example using a single ! followed by ==

string input = Console.ReadLine();

if (!((input == "0") || (input == "1")))
{
Console.WriteLine("not equal to 0 or 1");
}
else
{
Console.WriteLine("is equal to 0 or 1");
}

Console.ReadLine();
 
Hi,

I'm currently learning c# and am confused why the first example below
doesn't work but the second one does. I'd appreciate any feeback. Thanks.

First example using !=

string input = Console.ReadLine();

if ((input != "0") || (input != "1"))
{
Console.WriteLine("not equal to 0 or 1");
}
else
{
Console.WriteLine("is equal to 0 or 1");
}

Console.ReadLine();

Second example using a single ! followed by ==

string input = Console.ReadLine();

if (!((input == "0") || (input == "1")))
{
Console.WriteLine("not equal to 0 or 1");
}
else
{
Console.WriteLine("is equal to 0 or 1");
}

Console.ReadLine();

your line (first example):
if ((input != "0") || (input != "1"))

what if 'input' really is "1"? Then (input != "0") is true, so the
complete test is true!

|| is an "OR" operator, which means that at least one of the tests (on
both sides) should be "true" (with the special case that if the first
(left hand) test already results in "true", the second one never even
is tested: that would not influence the final result).

Hans Kesting
 
Hi,

I'm currently learning c# and am confused why the first example below
doesn't work but the second one does. I'd appreciate any feeback. Thanks.

First example using !=

                string input = Console.ReadLine();

                if ((input != "0") || (input != "1"))
                {
                    Console.WriteLine("not equal to 0 or 1");
                }
                else
                {
                    Console.WriteLine("is equal to 0 or 1");
                }

                Console.ReadLine();

Second example using a single ! followed by ==

                string input = Console.ReadLine();

                if (!((input == "0") || (input == "1")))
                {
                    Console.WriteLine("not equal to 0 or 1");
                }
                else
                {
                    Console.WriteLine("is equal to 0 or 1");
                }

                Console.ReadLine();

Matty-

In two words: DeMorgan's Law. Wiki it sometime, it might interest
you.

Basically, the second example works because you have this:

!((a=b) | (a=c))

which is not the same as

(a=!b) | (a=!c)

Try the following:

if((input !="0") && (input!="1")) ...

If you'd like to see a mathematical reason, I'd be happy to show you.

Hope this helps,
Ross
 
Instead of
if ((input != "0") || (input != "1"))
try
if ((input != "0") && (input != "1"))

Your test basically says "If input isn't "0" or it isn't "1" ...
Well it can't be both at the same time so your test is always true.

HTH,

AB
=======
 
Hi,

I'm currently learning c# and am confused why the first example below
doesn't work but the second one does. I'd appreciate any feeback. Thanks.

How it does not work?
What were you expecting?
if ((input != "0") || (input != "1"))

basically returns true IF input is not 0 or input is not 1. That means
that IF input is 0 it's true as 0 != 1 , the same thing the otherway
around. so in other words it will always be true !!!
so the only way it can fail is if input is 0 and 1, get your
conclusions from there.
 
Matty,

Simple because in the second sample you tell exactly what you want while the
first is rubish,

You cannot be fysical at the same time not a woman or not a man.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top