1

I wonder why this gives me an error message. What is the path that is missing? Equal to, less or greater than - that should cover all paths right? PS. I'm new to programming.

 public string LargestNumber(int num1, int num2)
    {
        if (num1 > num2)
            return("number 1 is the greatest!");
            
        if (num1 < num2)
            return("number 2 is the greatest!");
            
        if (num1 == num2)
            return("Both are equal!");
                
    }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Eric Rosén
  • 39
  • 1
  • 6
  • Please use `else if` – Markus Meyer Aug 24 '22 at 11:26
  • 3
    I would remove the `if (num1 == num2)` but leave the `return("Both are equal!");` - no need to test for the only other logical option. – phuzi Aug 24 '22 at 11:28
  • If I change to else if, the problem remains. – Eric Rosén Aug 24 '22 at 11:28
  • 1
    It seems that the analyser is not clever enough to figure out what we can easily see. – phuzi Aug 24 '22 at 11:30
  • 1
    "I wonder why this gives me an error message." Because the rules of the C# specification don't allow or require C# compilers to perform the kind of flow analysis you're expecting. Yes, *we* can reason that exactly one of those `if` conditions will be true for all values of `num1` and `num2`, but the C# compiler doesn't. – Jon Skeet Aug 24 '22 at 11:33
  • As an aside, I'd probably convert the whole thing into a conditional expression... https://gist.github.com/jskeet/0134c88445fea6cfecdc2ee873694431 – Jon Skeet Aug 24 '22 at 11:36
  • the last condition is redundant - just `return "Both are equal";` – Dmitry Bychenko Aug 24 '22 at 11:41
  • @JonSkeet Even if such flow analysis could even be constructed. For example, DmitryBychenko's example, where all of them fail. How could the compiler even begin to deal with overloaded comparison operators? – Charlieface Aug 24 '22 at 20:14
  • Does this answer your question? [C# compiler error: "not all code paths return a value"](https://stackoverflow.com/questions/21197410/c-sharp-compiler-error-not-all-code-paths-return-a-value) – Ibrennan208 Aug 24 '22 at 20:19

3 Answers3

4

Well, in many a cases (not int, but say double) we can have incomparable values where we can't say if they are equal or one of them is larger or smaller. The compiler aware of it (but it doesn't know the exact int comparison implementation) so it complains: what if num1 and num2 are incomparable? And all num1 > num2, num1 < num2, num1 == num2 return false? What should be returned in such a case?

The easiest solution for you is to drop the last condition:

public string LargestNumber(int num1, int num2)
{
    if (num1 > num2)
        return("number 1 is the greatest!");
            
    if (num1 < num2)
        return("number 2 is the greatest!");
            
    // We know, that there's one option here : to be equal 
    // Compiler doesn't know that all ints are comparable
    return("Both are equal!");
}

please, note that in case of the same code but for double the complainment makes sence. Incomparable double values exist:

public string LargestNumber(double num1, double num2)
{
    if (num1 > num2)
        return("number 1 is the greatest!");
            
    if (num1 < num2)
        return("number 2 is the greatest!");
            
    if (num1 == num2)
        return("Both are equal!");

    return "Oops!";
}

Demo:

// double.NaN - Not a Number
// if floating point value is not a number, we can't just compare it!
// all >, <, == will return false!
Console.Write(LargestNumber(double.NaN, double.NaN));

Output:

Oops!
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

I've now updated it and it seem to work..

 public string SumOfNumber(int num1, int num2)
    {
        if (num1 > num2)
            return("number 1 is the greatest!");
            
        else if (num1 < num2)
            return("number 2 is the greatest!");
            
        else 
            return("Both are equal!");
            
    }
    
Eric Rosén
  • 39
  • 1
  • 6
1

The compiler is not smart enough(better: it does not do all your work) to check that it's impossible that this value is never greater/smaller/equal than another value. It says you: do your work and ensure that it's impossible. Easy to fix though:

Just remove the last if:

public string LargestNumber(int num1, int num2)
{
    if (num1 > num2)
        return "number 1 is the greatest!";
        
    if (num1 < num2)
        return "number 2 is the greatest!";
        
    return "Both are equal!";
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939