SoFa7
SoFa7

Reputation: 19

or in if-statement doesn't work

I tried the following , I want to get a message if the entered number isn't 1, 2 or 3 :

Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (number != 1 || number != 2 || number != 3)
{
    Console.WriteLine("wrong number!");
}

but also if my input is 1,2 or 3 I get the message , but why ? whats wrong ? :/ there is no error by visual studio.

Upvotes: 0

Views: 781

Answers (4)

Siby Sunny
Siby Sunny

Reputation: 752

You should use AND operator in that if statement

if (number != 1 && number != 2 && number != 3)
{
    Console.WriteLine("wrong number!");
}

or in this case you can use

if (number <= 0 || number >= 4)
{
    Console.WriteLine("wrong number!");
}

Upvotes: 1

Donald Jansen
Donald Jansen

Reputation: 1985

If I take a look at your code

Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (number != 1 || number != 2 || number != 3)
{
    Console.WriteLine("wrong number!");
}

lets say number is now equal to 1 if (1 != 1 || 1 != 2 || 1 != 3) this will give the following using Truth tables if ( false || true || true ) this will then result in if (true)

For more information about truth tables you can visit http://kias.dyndns.org/comath/21.html but in short using the stroke

true || true = true

true || false = true

false || true = true

false || false = false

But if you look at using the ampersand true && true = true

true && false = false

false && false = false

This is the reason why you will always get to line Console.WriteLine("wrong number!"); because if you enter 1,2,3 you will get atleast 2 'True' values, you have a number of options that you can follow

1) if (number != 1 || number != 2 || number != 3) changes to if (number != 1 && number != 2 && number != 3) 2) make an array for valid values int[] valid = new int[] {1,2,3} Then test if the input is inside of the valid array using a for loop or the easy Linq method Contains(...)

Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (!valid.Contains(number))
{
    Console.WriteLine("wrong number!");
}

Note I have added a Not operator infront of valid (!), this means if valid does not contain the value

Upvotes: 0

Cullub
Cullub

Reputation: 3258

You're confusing English with C#.

TL;DR: Use the && operator to check for all three, instead of just checking for any of the three.


You're currently checking: if the number is either not one, or not two, or not three, then say "wrong number".

if (number != 1 || number != 2 || number != 3)

Take, for example, number = 1. The code will first check whether number != 1, and will return false. However, since you're using an OR statement, it will check the next one. What ends up happening is this:

if (false || true || true)

Since you're using OR, C# checks to see if ANY of those are true. So this further simplifies to the following:

if (true)

Since a number can not be !=1, !=2, and !=3 at the same time, this will always have at least two trues. Therefore, if you use the OR operator, the expression will always return true

What you want is the && operator.

//if number is NOT 1 AND NOT 2 AND NOT 3
if (number != 1 && number != 2 && number != 3)

With the example above, this would simplify to this:

if (false && true && true)

Which goes further to:

if (false)
    //wrong number

Which will skip the inside, because 1 is a right number. If 1, 2, and 3 are wrong numbers, just put an exclamation point before the entire block, as follows:

if (!(number != 1 && number != 2 && number != 3))

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222700

You should consider using && operator which will check for at least number does not belong to one of those

if (number != 1 && number != 2 && number != 3)
{
    Console.WriteLine("wrong number!");
}

Upvotes: 0

Related Questions