I'm writing an android app that does a lot of stuff. I recently refactored my code to have a better structure, but suddenly I'm getting a very strange problem.
handleRequest(String str)
{
boolean foo = executeCommand(str);
this.publishProgress("FOO1: " + foo);
if (foo == false);
{
this.publishProgress("FOO2: " + foo);
sendString("Failed to execute: " + str);
}
this.publishProgress("FOO3: " + foo);
sendEOM();
}
The code above should execute a command, and store 'foo' with if the command was executed correctly. This code is inside an Android AsyncTask (thread) so I use 'publishProgress' to show a toast.
I've been flipping through the debugger and FOO is true! The toasts show FOO to be true the entire way through as well. However, it goes ahead and jumps inside the IF block and executes that too. I've never seen this before, I think its a problem with Java. I was stepping though the function 'executeCommand' and it looks like it is skipping return statements too.
I've ran the code on a virtual device and a real one and they both do this.
Any ideas? I'm completely at a loss here.
If you are getting an error about the else it is because you've told the interpreter that the ; was the end of your if statement so when it finds the else a few lines later it starts complaining. A few examples of where not to put a semicolon: if (age < 18); if ( 9 > 10 ); if ("Yogi Bear".
1. What is the problem with IF statement? Explanation: The IF statement has a priority according to which conditions are being tested. Whenever it is found to be true, all the following ELSIF statements are skipped and the END IF is executed.
If the if-statement is True , the code section under the else clause does not run. The keyword else needs to be on its own line and be at the same indentation level as the if keyword that the else corresponds to. The keyword else needs to be followed by a colon : .
If you require code to run only when the statement returns true (and do nothing else if false) then an else statement is not needed. On the other hand if you need a code to execute “A” when true and “B” when false, then you can use the if / else statement.
You said
if (foo == false);
remove the semicolon, it should be
if (foo == false) {//your code}
Remove the semicolon: if (foo == false);
-> if (foo == false)
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