Here's my code simplified
private static boolean isTrue()
{
return false;
}
public void Update()
{
if (isTrue())
doSomething();
}
For some odd reason doSomething() gets always executed, no matter what isTrue() returns.
Why?
Edit: I had a semicolon somewhere off screen directly after closing the if statement like this:
if (false);
doSomething();
Like this doSomething() gets executed always.
For some odd reason DoSomething() gets always executed, no matter what IsTrue returns. Why?
This can be only if one or more of the following occurs:
doSomething().; after if. ✓Important note, more than the question: Please follow the Java Naming Conventions.
After your edit, I'll explain why it's always executed.
It's simply because
if(something);
{
System.out.println("I'll be always printed!");
}
Is equivalent to
if(something) { }
{
System.out.println("I'll be always printed!");
}
It's a good practice to always have { and }, even if the body of the if includes only one statement, it's clearer and will help you to avoid possible bugs in the future, like @GrijeshChauhan mentioned, if you want to add lines in the future.
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