Home > front end >  Equal to, less or greater then - what path am I missing? Why does is say "not all code paths re
Equal to, less or greater then - what path am I missing? Why does is say "not all code paths re

Time:08-24

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

CodePudding user response:

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

CodePudding user response:

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 compaler aware of it (but it doesn't know the exact int comparion implementation) so it complains: what if num1 and num2 are incompartibe? And all num1 > num2, num1 < num2, num1 == num2 return false? What should be returned? The easiest solution 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!

CodePudding user response:

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

CodePudding user response:

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"

CodePudding user response:

Please use else if. if -> else if -> else

On one side, it's more efficient. But it also makes clear which branch is used.

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

    return "Both are equal!";
}
  • Related