简体   繁体   中英

if statement executing even if condition is false

I am trying: if abb.iscurrentlyy() returns true, then go inside the if statement or abb.islately() is true.

What is wrong with my if statement? When I set abb.iscurrentlyy() to be false. abb.islately() is set to true. Why is abb.iscurrentlyy() going inside the loop even abb.iscurrentlyy() is false?

    if (abb.iscurrentlyy() || abb.islately()) {

        if (reqFiles != null) {

            for (int i = 0; i < reqFiles.length; i++) {

                Future<ExecResult> lcukv = executor.submit(worker);
                executionFutureException.add(lcukv);
            }
        }

    } else { // do if condition is false.
    }

The term or (||) means that for the conditional to be true at least one of the sub-conditions has to be true.

boolean condition1 = true;
boolean condition2 = false;

if(condition1 || condition2)
{
    System.out.println("One or both conditions were true");
}
else
{
    System.out.println("Both conditions were false");
}

In real world terms, imagine walking into an ice cream shop. You like strawberry ice cream. You also like chocolate ice cream. You hate all other flavors. If the shop has at least one of those flavors, you're happy.

Your code is analogous to the example above. If, on the other hand, you want false when one of the conditions is true and the other is false, use and (&&).

boolean condition1 = true;
boolean condition2 = false;

if(condition1 && condition2)
{
    System.out.println("Both conditions were true");
}
else
{
    System.out.println("At least one condition was false");
}

In this case, the ice cream shop has to have both flavors you like in order for you to be happy.

Think of or as being more permissive: "I can have this OR that to be true." and , however, sounds inclusive, but it's actually more exclusive: "I have to have this AND that to be true."

In your code both abb.isCurrentlyy() and abb.islately() needs to be false to not go inside loop. || works as a logical OR. which means if either one of the value is true statement is evaluated as True. I suggest you to write individual if loop for these two condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM