Hello, so I have a question of how this code works and why.
This is my code:
public boolean isPrime(int number) {
// checks if Prime is number
if (number == 2) {
return true;
} else if ( number < 2 ) {
return false;
}
for (int i = 2; i < number; i++) {
if ( number % i == 0) {
return false;
}
return true;
}
// return true;
}
This code wont compile without the last return (commented at the moment), and it says its missing a return value.
It will run, when I uncomment the last return statement, and it looks like it will give me the correct result no matter if i return true or false at the last statement.
Why is this? Shouldnt it just take the return statement from earlier whenever the iteration conditions are meet?
Why does it override the last return value and give me the correct result?