简体   繁体   中英

Equal to, less or greater then - what path am I missing? Why does is say "not all code paths return a value"?

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!");
                
    }

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!

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!");
            
    }
    

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!";
}

You should definitely use an else if construct with an else clause at the end. There is no default path in your code that will be executed "no matter what"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM