Ruski
Ruski

Reputation: 15

Why is the first condition of "else if" statement always false?

I am assuming this is some logic error I am overlooking and am hoping someone can enlighten me on the mechanics of Java in this scenario. The first condition in the if else statements (id == R.id.checkbox1) are highlighted by Android Studio as being always false, why?

@Override
public void onClick(View view) {

    int id = view.getId();
    int page = viewPager.getCurrentItem();
    boolean cBoxChecked = false;

    switch(page) {
        case 0: case 1: case 3: case 4: case 6: case 10:
            if(id == R.id.button2)
                answersStatus.set(page, true);
            else
                answersStatus.set(page, false);
        case 2:
            if(id == R.id.button1)
                answersStatus.set(page, true);
            else
                answersStatus.set(page, false);
        case 5: case 7:
            if(id == R.id.checkbox1 || id == R.id.checkbox4 && cBoxChecked)
                cBoxChecked = true;
                  //____________________
            else if(id == R.id.checkbox1 || id == R.id.checkbox4 && !cBoxChecked)
                  //^^^^^^^^^^^^^^^^^^^^
                answersStatus.set(page, true);
            else
                answersStatus.set(page, false);
        case 8:
            if(id == R.id.checkbox1 || id == R.id.checkbox5 && cBoxChecked)
                cBoxChecked = true;
                  //____________________
            else if(id == R.id.checkbox1 || id == R.id.checkbox5 && !cBoxChecked)
                  //^^^^^^^^^^^^^^^^^^^^
                answersStatus.set(page, true);
            else
                answersStatus.set(page, false);
        case 9:

    }
}

Upvotes: 0

Views: 1241

Answers (2)

Stephen C
Stephen C

Reputation: 719229

Here's one of the code snippets.

if(id == R.id.checkbox1 || id == R.id.checkbox4 && cBoxChecked)
    cBoxChecked = true;
else if(id == R.id.checkbox1 || id == R.id.checkbox4 && !cBoxChecked)
    answersStatus.set(page, true);
else
    answersStatus.set(page, false);

The "then" statement of the first if will be executed when EITHER id == R.id.checkbox1 is true OR when id == R.id.checkbox4 && cBoxChecked is true.

So if you get to the else if, that means that id == R.id.checkbox1 CANNOT BE true. That is what the compiler is saying.

In Java (and in most / all other programming languages I have encountered) precedence of && is higher than ||.

Maybe you meant to write this:

if ((id == R.id.checkbox1 || id == R.id.checkbox4) && cBoxChecked)
    cBoxChecked = true;
else if ((id == R.id.checkbox1 || id == R.id.checkbox4) && !cBoxChecked)
    answersStatus.set(page, true);
else
    answersStatus.set(page, false);

Upvotes: 3

Aman Chhabra
Aman Chhabra

Reputation: 3894

&& has more priority then ||

So when you are writing below statement:

if(id == R.id.checkbox1 || id == R.id.checkbox4 && cBoxChecked)

It will check either for

id == R.id.checkbox1 

or

id == R.id.checkbox4 && cBoxChecked

Now the other else if statement says:

else if(id == R.id.checkbox1 || id == R.id.checkbox4 && !cBoxChecked)

which will again be split into

id == R.id.checkbox1

or

id == R.id.checkbox4 && !cBoxChecked

As the first statement "id == R.id.checkbox1" is same and whenever it will be true, the statements inside if will be called instead of else if.

Hope that makes sense.

Upvotes: 2

Related Questions