P. Pur
P. Pur

Reputation: 11

Comparing two numbers (Greater than, equal to or less than) without if statements

I have a task on my C# programming book that i need to program a:

"Program that reads two numbers from the keyboard and prints if the number 1 is bigger than, equal to or less than number 2.

The reason i assume i can't use if statements is because they have not yet been covered. This task is given before the book teaches the if statements. I have been trying to figure this out, do i need to use the ? operator..? It would be easy if it asked only if it's less than or greater than, less than or equal to or equal to or greater than number 2, but now there's 3 different things.

The book already teached ? operator, but not CompareTo. I wouldn't believe it left me to guess the existence of if-statement, because all the tasks in it should be able to entirely be solved with things that have already been taught. The book is about to teach the if-statement basically just on the next page of this task though.

This is the furthest i've got :P

int number1 = Convert.ToInt32(Console.ReadLine());

int number2 = Convert.ToInt32(Console.ReadLine());

Upvotes: 1

Views: 5799

Answers (4)

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

This answer for case you want to eliminate comparisons, including ?:, may not be suitable for "before if statement" page in the book so.

To remove all visible conditions in the code you normally use map (outcome, action). In this case sounds like result of Int32.CompareTo would be good starting point, and since we have just 3 values (-1,0,1) simple array (instead of Dictionary) would work. We only need to adjust [-1,1] range to [0,2] to use for indexing of array:

 int value1 = 42;
 int value2 = 32;

 string[] messages = new[] { 
      "First less than second", // maps to -1
      "Both are equal", // maps to 0
      "First greater than second" // maps to 1
 };

 Console.WriteLine(messages[ 
      1 + value1.CompareTo(value2) // shift -1,1 range to 0,2
  ]);

If you can't use ComapreTo but can use bit manipulation you can implement that without conditions too (see Comparing two integers without any comparison and http://graphics.stanford.edu/~seander/bithacks.html, code below less generic as we know size of System.Int32 in bits):

int CompareToWithoutConditionals(int v1, int v2)
{
   int v = v1 - v2;
   return (v >> 31) - ((-v) >> 31);
}

Upvotes: 0

Eric Lippert
Eric Lippert

Reputation: 660493

The reason I assume I can't use if statements is because they have not yet been covered.

That's a pretty reasonable assumption.

Covering conditional expressions before conditional statements is an interesting pedagogical choice, but it is perfectly valid.

do i need to use the ? operator?

Yep. That's the conditional operator, so if you are expressing logic that runs conditionally, use it.

It would be easy if it asked only if it's less than or greater than, less than or equal to or equal to or greater than number 2, but now there's 3 different things.

Yep. The insight here is that the form of the conditional operator is:

condition ? consequence : alternative

where the operator produces a value, and where the three operands are also values. Since the conditional operator produces a value, and takes a value, then you can nest them. For instance:

condition ? 
  (condition ? consequence : alternative) : 
  alternative

Or

condition ? 
  consequence :
  (condition ? consequence : alternative)

Now there are three places a result can go instead of two.

Of course you can have as many as you like:

condition ? 
  (condition ? consequence : alternative) : 
  (condition ? consequence : alternative)

But nested conditionals like this are often considered poor style because they get hard to read.

Does that give you insight into how to solve the problem?

This is the furthest i've got :P

int number1 = Convert.ToInt32(Console.ReadLine());
int number2 = Convert.ToInt32(Console.ReadLine());

For a complete beginner that is reasonable code, but you will learn soon I hope that it is very poor style because it crashes easily. If your user puts in something that is not a number then your program terminates.

The better approach is to use TryParse, and to ask the user again if they enter a bad input. But since you have probably not yet learned about loops, you won't be able to write that code yet.

Your next lines of code should be:

string message = some conditional logic;
Console.WriteLine(message);

Can you fill in the logic?

Upvotes: 3

Ousmane D.
Ousmane D.

Reputation: 56469

Well, your requirement doesn't explicitly state not to use if statements so I don't see a reason why not to use it. But, you know better than me so, I'll let you decided on whether to use it or not.

Alternatively, you can do it with the ternary operation like this:

string result = number1 > number2 ? $"number {number1} is greater"
                : number2 > number1 ? $"number {number2} is greater" 
                : $"number {number1} and number {number2} are equal";
Console.WriteLine(result);

Upvotes: 1

Xtros
Xtros

Reputation: 467

There's three different outputs: greater than, equal to, less than. So even if conditional statements were allowed, they are not the best option.

int result = number1.CompareTo(number2);

If result is < 0, then you know that number1 is less than number2. If result is 0, then you know that number1 is equal to number2. If result is > 0, then you know that number1 greater than number2.

Documentation

Upvotes: 0

Related Questions