return type error "not all code paths return a value"
-
here is my code: using System; class one { public int a; int f; public bool e(int i) { for (f = 2; f <= i; f++) { if (i % f == 0) { return false; } else { return true; } } } } class control { public static void Main() { one obj = new one(); Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)"); int o=obj.a = int.Parse(Console.ReadLine()); obj.e(o); if (obj.e(o)) Console.WriteLine("THE VALUE IS PRIME"); else Console.WriteLine("THE VALUE IS NOT PRIME"); } } now when i debug this program it returns an error "not all code paths return a value" pointing at e. and gives a warning at f++ in the for statement "unreachable".....what is the fault??
I'll reformat your code so that we can read it:
using System;
class one
{
public int a;
int f;public bool e(int i)
{
for (f = 2; f <= i; f++)
{
if (i % f == 0)
{
return false;
}
else
{
return true;
}
}
}
}class control
{
public static void Main()
{
one obj = new one();
Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)");
int o=obj.a = int.Parse(Console.ReadLine());
obj.e(o);
if (obj.e(o))
Console.WriteLine("THE VALUE IS PRIME");
else
Console.WriteLine("THE VALUE IS NOT PRIME");
}
}First of all, why f++ is unreachable. In your code, the for loop will always exit after the first run through - there is no way for it not to. Remove the return true to outside the loop and you'll solve both parts of your problem - the loop condition will be reachable and the code will return a value from all parts. [Edit]I just want to point out that you need to code for an edge case here. What happens if I put 0 in as a value? The code will never enter the loop and will incorrectly report out that it's a prime number. You should add a check in here to ensure that it's not less than 2 and return the value accordingly.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
modified on Monday, May 10, 2010 7:27 AM
-
here is my code: using System; class one { public int a; int f; public bool e(int i) { for (f = 2; f <= i; f++) { if (i % f == 0) { return false; } else { return true; } } } } class control { public static void Main() { one obj = new one(); Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)"); int o=obj.a = int.Parse(Console.ReadLine()); obj.e(o); if (obj.e(o)) Console.WriteLine("THE VALUE IS PRIME"); else Console.WriteLine("THE VALUE IS NOT PRIME"); } } now when i debug this program it returns an error "not all code paths return a value" pointing at e. and gives a warning at f++ in the for statement "unreachable".....what is the fault??
While the other both answers are correct, I only want to mention the reasoning behind it. The code considers all possible exits in a program and if it doesn't achieve the desired result in it, it raises an error. In your for loop, you have trapped all possible outcomes. So, whatever the number f be, there will be an answer. But what happens when the code doesn't enter the loop? what if i = 1? Hence, there should be one more return statement after the loop ends.
-
I'll reformat your code so that we can read it:
using System;
class one
{
public int a;
int f;public bool e(int i)
{
for (f = 2; f <= i; f++)
{
if (i % f == 0)
{
return false;
}
else
{
return true;
}
}
}
}class control
{
public static void Main()
{
one obj = new one();
Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)");
int o=obj.a = int.Parse(Console.ReadLine());
obj.e(o);
if (obj.e(o))
Console.WriteLine("THE VALUE IS PRIME");
else
Console.WriteLine("THE VALUE IS NOT PRIME");
}
}First of all, why f++ is unreachable. In your code, the for loop will always exit after the first run through - there is no way for it not to. Remove the return true to outside the loop and you'll solve both parts of your problem - the loop condition will be reachable and the code will return a value from all parts. [Edit]I just want to point out that you need to code for an edge case here. What happens if I put 0 in as a value? The code will never enter the loop and will incorrectly report out that it's a prime number. You should add a check in here to ensure that it's not less than 2 and return the value accordingly.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
modified on Monday, May 10, 2010 7:27 AM
take a look at this code.. using System; class one { public int a; int f; public bool e(int i) { if (i <= 2) return false; } } class control { public static void Main() { one obj = new one(); Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)"); int o = obj.a = int.Parse(Console.ReadLine()); obj.e(o); if (obj.e(o)) Console.WriteLine("THE VALUE IS PRIME"); else Console.WriteLine("THE VALUE IS NOT PRIME"); } just look at the upper portion....it still doesnt return a value and gives the same error now look at this code using System; class ChkNum { // Return true if x is prime. public bool IsPrime(int x) { if(x <= 1) return false; for(int i=2; i <= x/i; i++) if((x %i) == 0) return false; return true; } } class ParmDemo { static void Main() { ChkNum ob = new ChkNum(); for(int i=2; i < 10; i++) if(ob.IsPrime(i)) Console.WriteLine(i + " is prime."); else Console.WriteLine(i + " is not prime."); } } now look at this code(the upper section)..in this also it uses "if(x <= 1) return false;" in this code this statement works...but not in the first one...why is that!!
-
While the other both answers are correct, I only want to mention the reasoning behind it. The code considers all possible exits in a program and if it doesn't achieve the desired result in it, it raises an error. In your for loop, you have trapped all possible outcomes. So, whatever the number f be, there will be an answer. But what happens when the code doesn't enter the loop? what if i = 1? Hence, there should be one more return statement after the loop ends.
take a look at this code.. using System; class one { public int a; int f; public bool e(int i) { if (i <= 2) return false; } } class control { public static void Main() { one obj = new one(); Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)"); int o = obj.a = int.Parse(Console.ReadLine()); obj.e(o); if (obj.e(o)) Console.WriteLine("THE VALUE IS PRIME"); else Console.WriteLine("THE VALUE IS NOT PRIME"); } just look at the upper portion....it still doesnt return a value and gives the same error now look at this code using System; class ChkNum { // Return true if x is prime. public bool IsPrime(int x) { if(x <= 1) return false; for(int i=2; i <= x/i; i++) if((x %i) == 0) return false; return true; } } class ParmDemo { static void Main() { ChkNum ob = new ChkNum(); for(int i=2; i < 10; i++) if(ob.IsPrime(i)) Console.WriteLine(i + " is prime."); else Console.WriteLine(i + " is not prime."); } } now look at this code(the upper section)..in this also it uses "if(x <=1) return false;" in this code this statement works...but not in the first one...why is that!!
-
take a look at this code.. using System; class one { public int a; int f; public bool e(int i) { if (i <= 2) return false; } } class control { public static void Main() { one obj = new one(); Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)"); int o = obj.a = int.Parse(Console.ReadLine()); obj.e(o); if (obj.e(o)) Console.WriteLine("THE VALUE IS PRIME"); else Console.WriteLine("THE VALUE IS NOT PRIME"); } just look at the upper portion....it still doesnt return a value and gives the same error now look at this code using System; class ChkNum { // Return true if x is prime. public bool IsPrime(int x) { if(x <= 1) return false; for(int i=2; i <= x/i; i++) if((x %i) == 0) return false; return true; } } class ParmDemo { static void Main() { ChkNum ob = new ChkNum(); for(int i=2; i < 10; i++) if(ob.IsPrime(i)) Console.WriteLine(i + " is prime."); else Console.WriteLine(i + " is not prime."); } } now look at this code(the upper section)..in this also it uses "if(x <=1) return false;" in this code this statement works...but not in the first one...why is that!!
IMPORTANT: Use 'pre' tags for your code. without those tags, the code is totally unreadable. Also, do preview your post. There are many errors in your post. Now, the second code works because it considers all possible values of x. if it is less than 1, it returns false. otherwise, for all other values, it can be either true or false. try changing the code to
x<=0
-
take a look at this code.. using System; class one { public int a; int f; public bool e(int i) { if (i <= 2) return false; } } class control { public static void Main() { one obj = new one(); Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)"); int o = obj.a = int.Parse(Console.ReadLine()); obj.e(o); if (obj.e(o)) Console.WriteLine("THE VALUE IS PRIME"); else Console.WriteLine("THE VALUE IS NOT PRIME"); } just look at the upper portion....it still doesnt return a value and gives the same error now look at this code using System; class ChkNum { // Return true if x is prime. public bool IsPrime(int x) { if(x <= 1) return false; for(int i=2; i <= x/i; i++) if((x %i) == 0) return false; return true; } } class ParmDemo { static void Main() { ChkNum ob = new ChkNum(); for(int i=2; i < 10; i++) if(ob.IsPrime(i)) Console.WriteLine(i + " is prime."); else Console.WriteLine(i + " is not prime."); } } now look at this code(the upper section)..in this also it uses "if(x <= 1) return false;" in this code this statement works...but not in the first one...why is that!!
well in the first case there is still a path through the method that doesnt assign a return value you know its like what if i is more than 2 let me just break off here to say how difficult it is to read unformatted code whatever thats just me in the second case all paths through the method assign a return value hope thats cleared things up for u [EDIT fixed typo]
-
IMPORTANT: Use 'pre' tags for your code. without those tags, the code is totally unreadable. Also, do preview your post. There are many errors in your post. Now, the second code works because it considers all possible values of x. if it is less than 1, it returns false. otherwise, for all other values, it can be either true or false. try changing the code to
x<=0
-
that maybe...but i just wrote this code in the upper section
public bool e(int i)
{ if (i <= 2) return false; }
even then it gives the same error!!!....wats wrong in this code??
because the method returns a value only when i<=2. what if i>2? think how the code will proceed? where does the code get return statement if i>2?
-
because the method returns a value only when i<=2. what if i>2? think how the code will proceed? where does the code get return statement if i>2?
alrite.....see this code
using System;
class one
{
public int a;
// Return true if x is prime.
public bool e(int x)
{
if (x <= 2 && x>=0) return true;
for (int i = 2; i<;x; i++)
if (x % i == 0) return false;
return true;
}
} class control {
public static void Main()
{
one obj = new one();
Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)");
int o = obj.a = int.Parse(Console.ReadLine());
obj.e(o);
if (obj.e(o))
Console.WriteLine("THE VALUE IS PRIME");
else
Console.WriteLine("THE VALUE IS NOT PRIME");
}
} now see this code.....
using System;
class one
{
public int a;
public bool e(int i)
{
if (i <= 2 && i >= 0) return true;
for (int f = 2; f {
if (i % f == 0)
{
return false;
}
else
{
return true;
} } } } wats the difference between the two??? the upper one works the lower one gives the same error!!!
-
alrite.....see this code
using System;
class one
{
public int a;
// Return true if x is prime.
public bool e(int x)
{
if (x <= 2 && x>=0) return true;
for (int i = 2; i<;x; i++)
if (x % i == 0) return false;
return true;
}
} class control {
public static void Main()
{
one obj = new one();
Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)");
int o = obj.a = int.Parse(Console.ReadLine());
obj.e(o);
if (obj.e(o))
Console.WriteLine("THE VALUE IS PRIME");
else
Console.WriteLine("THE VALUE IS NOT PRIME");
}
} now see this code.....
using System;
class one
{
public int a;
public bool e(int i)
{
if (i <= 2 && i >= 0) return true;
for (int f = 2; f {
if (i % f == 0)
{
return false;
}
else
{
return true;
} } } } wats the difference between the two??? the upper one works the lower one gives the same error!!!
What are you trying to do here buddy? Are you trying to prove that your inefficient coding techniques are right and C# language is incorrect? Every programming language has a syntax. You need to follow them in order to use it. You need to ask these questions yourself and tell us why one code works and why the other doesn't. When you can find the reason yourself, you will be able to write the correct code from next time onwards. All the best.
-
What are you trying to do here buddy? Are you trying to prove that your inefficient coding techniques are right and C# language is incorrect? Every programming language has a syntax. You need to follow them in order to use it. You need to ask these questions yourself and tell us why one code works and why the other doesn't. When you can find the reason yourself, you will be able to write the correct code from next time onwards. All the best.
i'm nt condeming any body..... i just dont understand the difference...the syntax and logic in both codes are same..but one gives the error and the other doesnt....never before i have encountered such a problem....there is another way to do this program...but i wanted to use return types....and now it gives this error ....i am greatful to all your replies!!!
-
i'm nt condeming any body..... i just dont understand the difference...the syntax and logic in both codes are same..but one gives the error and the other doesnt....never before i have encountered such a problem....there is another way to do this program...but i wanted to use return types....and now it gives this error ....i am greatful to all your replies!!!
public bool e(int x) { if (x <= 2 && x >= 0) return true; //Some values return here for (int i = 2; i >= x; i++) if (x % i == 0) return false; //Some values return here return true; //Anything that is left is returned here }
This is your first piece of code which works. I have only reformatted it to see the code flow. Here, for every value of x, there is a return path available. Hence this one is a legal code.
public bool e(int i) { if (i <= 2 && i >= 0) return true; // if i = 0, 1, 2 return here. for (int f = 2; f <= i; f++) // if i >2 enter here. { if (i % f == 0) { return false; // only if i>2, some return here } else { return true; // only if i>2, BALANCE return here } } //What happens to values of i<0 ? }
As correctly pointed out by the compiler, not all paths return the value. Hence the code syntax is incorrect. I suggest you think a bit more logically to see the difference in two cases. I also suggest to take a break from coding. it happens sometimes when you have done coding for a very long time. trust me. Som
-
i'm nt condeming any body..... i just dont understand the difference...the syntax and logic in both codes are same..but one gives the error and the other doesnt....never before i have encountered such a problem....there is another way to do this program...but i wanted to use return types....and now it gives this error ....i am greatful to all your replies!!!
They are not the same. They just look it because you don't see what is happening. Indenting your code shows why they are different:
public bool e(int x)
{
if (x <= 2 && x>=0)
return true;
for (int i = 2; i<;x; i++)
if (x % i == 0)
return false;
return true;
}and
public bool e(int i)
{
if (i <= 2 && i >= 0)
return true;
for (int f = 2; f <= i; f++)
{
if (i % f == 0)
{
return false;
}
else
{
return true;
}
}
}In the first case, there is always a return statement - in every exit path. In the second case, the compiler assumes there is an exit path at the end of the loop that does not have a return statement. It doesn't need one, because your loop is crap - it will always return on the first iteration. But the compiler doesn't know that. It does not look at "i % 1" and say: that will always be 0 or 1, so there is a control path that ends in a return for all cases. It just looks and sees a control path after the loop that will never include a return. Surprisingly, the compiler is not as intelligent as you.
I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.
-
They are not the same. They just look it because you don't see what is happening. Indenting your code shows why they are different:
public bool e(int x)
{
if (x <= 2 && x>=0)
return true;
for (int i = 2; i<;x; i++)
if (x % i == 0)
return false;
return true;
}and
public bool e(int i)
{
if (i <= 2 && i >= 0)
return true;
for (int f = 2; f <= i; f++)
{
if (i % f == 0)
{
return false;
}
else
{
return true;
}
}
}In the first case, there is always a return statement - in every exit path. In the second case, the compiler assumes there is an exit path at the end of the loop that does not have a return statement. It doesn't need one, because your loop is crap - it will always return on the first iteration. But the compiler doesn't know that. It does not look at "i % 1" and say: that will always be 0 or 1, so there is a control path that ends in a return for all cases. It just looks and sees a control path after the loop that will never include a return. Surprisingly, the compiler is not as intelligent as you.
I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.
ohhh rite!!!!!!! My God i'm literally blind at the moment.....!!!! such a small error!!!!! and the else was there in the for bracket!!!!...(no wonder why it was saying unreachable); so it only see's the exits!!!!!!!!!!!!!!!!hmmmmmmmm..!!!! now i finally understnd!!! @som shekhar ya i tuk a 1 hour rest..............it paid off!!!!! thnx all for your support!!!!!!!