-1

My code was working perfectly before and now it doesn't compile anymore. I have searched for the missing return statement for a while but still can't find it...Here is the code:

private static boolean Prime(int x){        
for(int i=1;i<x;i++) 
{
if(x%i==0){
return false;
}   
return true;
}
}
}
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Nicolas
  • 17
  • 2
  • 5
    Is there any particular reason why you do not indent your code? You'd be amazed how easily you'd be able to spot the error. (Also, this isn't JavaScript to begin with...) – Tomalak Feb 06 '12 at 23:10
  • (To editors: please do not try to improve the question by indenting the code. The fact that the code is not indented is the whole *point* of the question.) – Tomalak Feb 06 '12 at 23:30
  • 1
    I'll indent my code from now on...Thanks – Nicolas Feb 07 '12 at 00:11
  • Which programming language? – Raedwald Jan 02 '15 at 23:26
  • Possible duplicate of http://stackoverflow.com/questions/23058029/missing-return-statement-within-if-for-while – Raedwald Jan 02 '15 at 23:27

2 Answers2

2

shouldn't the last return be one closing curly brace later?

private static boolean Prime(int x){        
    for(int i=1;i*i<x;i++) 
    {
        if(x%i==0){
            return false;
        }   
    }
    return true;
}

and, as a side note, you could stop the for loop at the square root of x.

i * i < x

or

i < sqrt(x)

I updated the condition in the indented code block.

Jörg Beyer
  • 3,631
  • 21
  • 35
1

It's easy to see a problem if you use indentations:

private static boolean Prime(int x) {
    for (int i = 1; i < x; i++) {
        if (x % i == 0) {
            return false;
        }
        return true;
    }
}
}
Yuriy Zubarev
  • 2,821
  • 18
  • 24