bfahm
bfahm

Reputation: 318

Java - Condition is always True/False (Android Checkboxes)

I have the following code to display a certain string according to which checkboxes are checked in my Android app:

public String createOrderSummary(){

    CheckBox whippedCheckBox = findViewById(R.id.whipped);
    boolean whippedCream = whippedCheckBox.isChecked();

    CheckBox chocoBox = findViewById(R.id.chocolate);
    boolean chocolate = chocoBox.isChecked();

    if (whippedCream && chocolate){
        return "both selected";
    }else if(whippedCream || chocolate){
        if (whippedCream){
            return "whippedcream";
        }else if (chocolate){
            return "chocolate";
        }
    }else{
        return "none checked";
    }
    return "";
}

I receive a warning at line 14 saying condition chocolate is always true, why is that?

Also when I switch lines to be:

 if (whippedCream){
        return "whipped cream";
    }else if(whippedCream && chocolate){
        return "both selected";
    }else{
        return "none selected";
    }

I receive warning at line 3 saying condition always false.

Upvotes: 0

Views: 2666

Answers (4)

Xenolion
Xenolion

Reputation: 12717

It is logical phenomenon:

If this condition is true: if(whippedCream || chocolate) That means one of them either whippedCream or chocolate is true or both.

So inside it there is if (whippedCream) else if(chocolate) that means the second if in the else if is unnecessary. Because one of them is true, so if(whippedCream)is false then the else means chocolate is true and the condition if(chocolate) is unnecessary.

Upvotes: 0

Dan Harms
Dan Harms

Reputation: 4840

The message is because of how you have nested your ifs. You are entering if (whippedCream || chocolate) only if one of them is true. You then check each one individually when a simple else would suffice.

Upvotes: 0

ShaneCoder
ShaneCoder

Reputation: 781

Let's consider the code

}else if(whippedCream || chocolate){
    if (whippedCream){
        return "whippedcream";
    }else if (chocolate){
        return "chocolate";
    }

We'll only check if (whippedCream) if whippedCream or chocolate are true. So, when we get to the else if (chocolate), then we know whippedCream is false, or we would not be in the 2nd else. We also know chocolate is true or we wouldn't be in this block.

Upvotes: 0

Eran
Eran

Reputation: 393771

Let's consider part of your condition:

if (whippedCream || chocolate) { // either whippedCream is true or chocolate is true
    if (whippedCream) { // whippedCream is true
        return "whippedcream";
    } else if (chocolate) { // whippedCream is not true, so chocolate must be true
        return "chocolate";
    }
}

therefore this condition can be simplified:

if (whippedCream || chocolate) { // either whippedCream  is true or chocolate is true
    if (whippedCream) { // whippedCream is true
        return "whippedcream";
    } else { // chocolate must be true
        return "chocolate";
    }
}

Of course, the full condition can be further simplified by eliminating the inner condition:

if (whippedCream && chocolate) { // both true
    return "both selected";
} else if (whippedCream) { // only whippedCream is true
    return "whippedcream";
} else if (chocolate) { // only chocolate is true
    return "chocolate";
} else {
    return "none checked";
}

Your alternative condition:

if (whippedCream){
    return "whipped cream";
}else if(whippedCream && chocolate){
    return "both selected";
}else{
    return "none selected";
}

is simply wrong. If whippedCream is true, you'll never check if both whippedCream and chocolate are true, since the condition of else if is only evaluated if all the preceding conditions are false.

Upvotes: 4

Related Questions