Mike.Chun
Mike.Chun

Reputation: 378

How can I write a if statement with multiple conditions?

I am trying to write a if statement where if a is true and if either (b or c) is true then do something.

I've written this but I am not sure if the logic of it is correct.

if (critStatus == false && badStatus == true || pmBadStatus == true) {
//do something
}

Basically if critStatus is false && if badstatus or pmbadstatus is true then it should do something.

Upvotes: 0

Views: 1018

Answers (3)

Caroline Hardison
Caroline Hardison

Reputation: 25

if (critStatus == false && (badStatus == true || pmBadStatus == true)) {
//do something
}

all you need is parenthesis. before the parenthesis the statement was:

if ((a = true and b = true) OR c = true){

Upvotes: 0

badjr
badjr

Reputation: 2296

&& has higher precedence than ||, so you have to write your condition like this:

if (critStatus == false && (badStatus == true || pmBadStatus == true))

See the Java precedence rules here.

Upvotes: 1

(A&&B)||C or A&&(B||C) are not the same evaluated, the order will be depending where are the parenthesis.

You mean

if (!critStatus && (badStatus || pmBadStatus)) {
    //do something
}

Upvotes: 1

Related Questions