&& (And) and || (Or) in If Statements

&& (AND) and || (OR) in IF statements

No, it will not be evaluated. And this is very useful. For example, if you need to test whether a String is not null or empty, you can write:

if (str != null && !str.isEmpty()) {
doSomethingWith(str.charAt(0));
}

or, the other way around

if (str == null || str.isEmpty()) {
complainAboutUnusableString();
} else {
doSomethingWith(str.charAt(0));
}

If we didn't have 'short-circuits' in Java, we'd receive a lot of NullPointerExceptions in the above lines of code.

If with multiple &&, || conditions Evaluation in java

If I understand you correctly, in the first part of your question, you are asking whether a and b must both be true for the entire expression (a && b || c || d || e) to evaluate as true.

The answer to that question is no. Java operators are such that && has higher precedence than ||. So the equivalent expression is:

(a && b) || c || d || e

Therefore the expression as a whole will evaluate to true if any of a && b or c or d or e is true. For example, if a was false and c was true, the expression as a whole is true.

Note that Java conditional operators short-circuit. This means that once the end result of the expression is known, evaluation stops. For example, if a and b were both true, meaning the overall expression must evaluate to true, then c, d and e are not evaluated.

For the second part of your question, we can apply the same logic. The equivalent expression is therefore:

(a && b) || (a && c) || (a && d) || (a && e)

This will evaluate to true if any of the sub-components, eg. a && c evaluates to true. As others have noted, a must be true for the expression to be true as it is part of every sub-component. Then if any of the other variables is true, the expression is true.

Once you understand that, you can see how the simplification of this expression suggested by @Arc676 is arrived at:

a && (b || c || d || e)

I should also add that the two expressions in your question are different logically. They are equivalent to:

(a && b) || c || d || e

and

a && (b || c || d || e)

The parentheses affect the order of evaluation. In the first, a and b are grouped together by an &&, hence both must be true for that part of the expression (in the parentheses) to be true. In the second, b, c, d and e are grouped together by ||. In this case, only one of b, c, d and e needs to be true for that part of the expression (in the parentheses) to be true.

can you have two conditions in an if statement

You can use logical operators to combine your boolean expressions.

  • && is a logical and (both conditions need to be true)
  • || is a logical or (at least one condition needs to be true)
  • ^ is a xor (exactly one condition needs to be true)
  • (== compares objects by identity)

For example:

if (firstCondition && (secondCondition || thirdCondition)) {
...
}

There are also bitwise operators:

  • & is a bitwise and
  • | is a bitwise or
  • ^ is a xor

They are mainly used when operating with bits and bytes. However there is another difference, let's take again a look at this expression:

firstCondition && (secondCondition || thirdCondition)

If you use the logical operators and firstCondition evaluates to false then Java will not compute the second or third condition as the result of the whole logical expression is already known to be false. However if you use the bitwise operators then Java will not stop and continue computing everything:

firstCondition & (secondCondition | thirdCondition)

How to use || and && together in one if statement

This is the correct answer:

if ((firstday == 5 || firstday == 6) && totalDays == 31){
//Do something
}

If statements and && or ||

It is entering the statement because the statement becomes true when it calculates iNumber != 9

An || (Or Operator) in an if will be true if any statement is true.

Think of it this way..

8 != 8 is False
8 != 9 is True

if ( False || True )
{
//Do Stuff
}

How/Can you use both && and || in the same if statement condition?

The operator && has a higher precedence than ||, so && will be evaluated first.

http://introcs.cs.princeton.edu/java/11precedence/

Still, many programmers will not remember that fact. It is clearer and more maintenance-friendly to use parenthesis to specifically state the order of evaluation intended.

Note that in your code you write

x=y

that is actually the assignment operator, not the equality operator. Presumably you intend

x==y

IF statement with logical && and || operators

The sub-statement of this if statement

} else if ((number1 && number2 > rndNumber) || (number1 && number3 > rndNumber) ||
(number2 && number3 > rndNumber)) {
printf("else if!\n");
}

will be executed when either number2 is greater than rndNumber or when number3 is greater than rndNumber provided that neither number1 or number2 is equal to zero.

For this numbers

1, 11, 1 or 1, 1, 11

this condition is valid.

For this numbers

11, 1, 1 or 1, 1, 1

the condition is not valid.

Pay into account that for example this sub-condition

(number1 && number2 > rndNumber)

is equivalent to

(number1 != 0 && number2 > rndNumber)


Related Topics



Leave a reply



Submit