So I was trying to figure out if the the first iteration in a for loop does not go through the termination condition.
Since when I called it in the main method with an input of 4 IsPrime(4)
the for loop still went through. I was expecting it to not go through since i = 2
and n/2 = 4/2 = 2
which will be 2 == 2
which will meet the condition to terminate but it went through and I got the right output but I don't get why it did not fail.
Please help.
public static boolean isPrime(int n){
if(n <= 1){
return false;
}
for(int i = 2; i <= n/2; i++){
if(n % i == 0){
return false;
}
}
return true;
}