Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF statement is just not working

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.

like image 620
yellow Avatar asked Jul 17 '12 15:07

yellow


People also ask

Why is my if-else statement not working?

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".

What is the problem with IF statement?

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.

Why is my if-else statement not working Python?

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 : .

Can you have just an if statement?

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.


2 Answers

You said

 if (foo == false);

remove the semicolon, it should be

 if (foo == false) {//your code}
like image 101
kosa Avatar answered Sep 18 '22 15:09

kosa


Remove the semicolon: if (foo == false); -> if (foo == false)

like image 43
Jack Satriano Avatar answered Sep 20 '22 15:09

Jack Satriano