I'm getting a dead code warning in a for loop at i++
. Why do I get that, and how do I solve this problem?
public static boolean Method(int p) {
for(int i = 2; i < p; i++) { // here is the problem, at i++
if(p % i == 0);
return false;
}
return true;
}
Solution 1 First thing you need to do is move the length test outside of the loop. If only letters and numbers are valid it would be easier to just test to see if each of the characters in the pass string is alphabetic or numeric using the isLetterOrDigit function.
What is dead Code. The unreachable code in method 2 is called “Dead code“. This is purely Eclipse compiler reported error, and if you will compile above class with “javac“, Java inbuilt compiler will only complain for other two methods.
Dead code is code that is never executed. This can be a method that's no longer called, a commented out block of code, or code appearing after a return statement that's unreachable. In any case, it often reflects functionality that no longer exists in the software and offers no value.
Yes you can use two condition in while using logical &&, || . As in above statement two conditions are being checked that is while loop will run either when strength is less than 100 or ht should be greater than 10. We can also use and (&&) as per the situation.
You always exit the loop immediately, hence i
never gets incremented.
if(p % i == 0);
return false;
should be
if(p % i == 0)
return false;
In the first version you have an empty clause following the if statement (due to the first semi-colon). Consequently the return false
always executes. You exit the method, and the i++
never executes.
Remove the semicolon after your if
statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With