FCain 1026
FCain 1026

Reputation: 59

"Unreachable Code Detected" Error Message in C#

I am trying to write a program in C# to find out if a number is a prime number. I use the first two if statements to single out many of the options, and then I am trying to nest a loop within the final else statement, and it tells me that there is 'unreachable code detected'.

public static bool primeNumber ()
    {
        Console.Write ("Please enter a number to see if it is a prime number: ");
        int num = int.Parse (Console.ReadLine ());
        if (num % 2 == 0)
            return false;
        else if (num % 5 == 0)
            return false;
        else {
            for (int i = 3; i < num / 2; i += 2)
            {
                if (num % i == 0)
                    return false;
                else 
                    return true;
            }
        }
    }

Upvotes: 0

Views: 8330

Answers (2)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

There are two problems here:

  • Unreachable code
  • Method doesn't return a value in all cases

Whereas the return value problem is an error, and will stop your compile from completing, the unreachable code is just a warning. By "just" I mean that it won't necessarily stop your compile, but you should treat it as an indication of a bug.

Let me write a new example that shows the actual problem here:

public static bool primeNumber (int num)
{
    for (int i = 0; i < num; i++)
    {
        if (i < 10)
            return false;
        else 
            return true;
    }
}

Let's see what this method does:

  1. It starts a loop
  2. First thing in the loop it checks something
  3. Depending on this something, it returns this or that. It returns

Thus, it never gets to this bit:

for (int i = 0; i < num; i++)
                         ^^^

this is the unreachable code.

Second, the compiler in your case cannot tell if num is always going to be a value that allows the loop to run, thus always returning something. As such, the compiler thinks that there is a chance you have a num (in my example it could be -1) value which will not allow the loop to run, making the program skip the loop, and thus we end up with the other error, what should the method return then?

If we go back to your code, here is a way to rewrite the method

public static bool primeNumber ()
{
    Console.Write ("Please enter a number to see if it is a prime number: ");
    int num = int.Parse (Console.ReadLine ());
    if (num % 2 == 0)
        return false;
    else if (num % 5 == 0)
        return false;
    else {
        for (int i = 3; i < num / 2; i += 2)
        {
            if (num % i == 0)
                return false;
        }
        return true;
    }
}
  • By only returning in the loop if we hit upon a number that proves the test (ie. it is not a prime number), we will remove the unreachable code problem
  • then, if we manage to run the loop to completion, we couldn't prove that it was not a prime number, thus it is a prime number, and then we return true.

Opinion: You shouldn't have a function that prompts the user and then returns the answer. Instead I would write this (LINQPad) program:

void Main()
{
    Console.Write ("Please enter a number to see if it is a prime number: ");
    int num = int.Parse (Console.ReadLine ());
    if (primeNumber(num))
        Console.WriteLine(num + " is a prime number");
    else
        Console.WriteLine(num + " is a not prime number");
}

public static bool primeNumber (int num)
{
    if (num % 2 == 0)
        return false;
    else if (num % 5 == 0)
        return false;
    else {
        for (int i = 3; i < num / 2; i += 2)
        {
            if (num % i == 0)
                return false;
        }
        return true;
    }
}

Upvotes: 12

mihai
mihai

Reputation: 4722

Your for loop does nothing here, as it returns on the first run, so

i += 2

is never reached.

Your whole for loop is equivalent to a single return true.

Upvotes: 1

Related Questions