471,348 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

equals confusion

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();
Jun 27 '08 #1
7 908
on 13-6-2008, Matty supposed :
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
Jun 27 '08 #2
On Jun 13, 11:25*am, Matty <Ma...@discussions.microsoft.comwrote:
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.
Look up De Morgan's Laws. The second example transposed is equivalent
to:

input != 0 AND input != 1

The first example is

input != 0 OR input != 1

http://en.wikipedia.org/wiki/De_Morgan%27s_Law
Jun 27 '08 #3
On Jun 13, 10:25*am, Matty <Ma...@discussions.microsoft.comwrote:
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
Jun 27 '08 #4
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
=======
Jun 27 '08 #5
On Jun 13, 11:25*am, Matty <Ma...@discussions.microsoft.comwrote:
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.
Jun 27 '08 #6
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

"Matty" <Ma***@discussions.microsoft.comschreef in bericht
news:93**********************************@microsof t.com...
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();
Jun 27 '08 #7
Many thanks for all the replies. This really helped.

Matty
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Zeng | last post: by
4 posts views Thread by Kurt | last post: by
7 posts views Thread by =?Utf-8?B?QWxleCBDb2hu?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.