473,242 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 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 956
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
17
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
4
by: Kurt | last post by:
Wouldn't you agree all of the follwoing should produce the same result? r = (o1 == o2); r = (o2 == o1); r = object.Equals(o1, o2); r = object.Equals(o2, o1); r = (o1.Equals(o2)); r =...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
2
by: Dot net work | last post by:
Hello, My simple code is here: Public Class MyDictionary Inherits System.Collections.DictionaryBase Private Class MyElement Public Overloads Overrides Function Equals(ByVal obj As Object)...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
7
by: =?Utf-8?B?QWxleCBDb2hu?= | last post by:
In C++, there is an easy technique to provide an overloaded Equals() method. A straightforward translation to C# causes a stack overflow. Why does b.Equals(ba) in the snippet below not understand...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.