6

Im not understanding the difference between the && and the || in a certain regard.

If(a && b) { then do this } 

Now if a is false then b Is looked at, but the condition is not tested.

But....

If(a || b) { then do this } 

Now, if a is true then b Is not looked at, and the condition is not tested.

Why is this. I thought the purpose of the && and the || was to sort of a help speed things along. If it could determine a result by testing the first condition, it wouldnt need look at the second condition. But in the case of && it does look at it. While in the case of || it dosent.

Do i have this right? If i do, why is this?

3
  • 4
    See stackoverflow.com/questions/8759868/… Commented Nov 26, 2013 at 3:34
  • 4
    "Now if a is false then b Is looked at, but the condition is not tested." How exactly did you determine this to be true? Commented Nov 26, 2013 at 3:37
  • See the duplicate, and read about Truth Tables for logical conjunction (&&) and logical disjunction (||) (en.wikipedia.org/wiki/Truth_table#Logical_conjunction) Commented Nov 26, 2013 at 3:40

5 Answers 5

13

To understand the answer, remind yourself of the truth tables of AND and OR operations:

 AND true | false \--------+------- true | true | false ------+-------+------- false | false | false OR true | false \--------+------- true | true | true ------+-------+------- false | true | false 

Note the second column for AND and the first column for OR: they have identical values. This is the key to understanding the reason why AND ignores its second argument when the first evaluates to false: that's when the second argument does not matter (the second column). The opposite is true for the OR operator: now a false in the first argument means that the second one does matter. However, a true in the first argument (the first column) means that the second argument can be ignored.

I thought the purpose of the && and the || was to sort of a help speed things along.

That's a smaller part of their purpose. Much bigger one is protection of right side of an expression based on its left side. Consider a situation where you want to guard against empty and null strings. If && and || didn't "short circuit" the evaluation, you would be forced to write code like this:

if (s == null) return false; if (s.length() == 0) return false; 

The reason you can write

if (s == null || s.length() == 0) return false; 

is the short circuiting: there is a guarantee that s.length() would not be invoked on a null string.

Sign up to request clarification or add additional context in comments.

Comments

5

&& is used to perform and operation means if anyone of the expression/condition evaluates to false whole thing is false.

so if any one of the condition becomes false it won't even continue further to check 

|| is used to perform or operation if anyone of the expression/condition evaluates to true whole thing becomes true.

so it continues till the end to check atleast one condition to become true. 

5 Comments

uhm... read his question again carefully.
This is *not what OP asked about.
I understand this. But in the case of && the 2nd condition is looked at regardless of whether the first condition is true or false. Why?
Ok. My teacher has mislead me. Setting a condition to false as in (a=false) had me thinking this condition is true because "a" can accept false. Im a student, and my teachers are non university educated. Im sorry, this has all been a mistake.
@ragingbull: The best thing might be to delete the question so it doesn't lead to further confusion.
2

&& is AND where as || is OR operator. && looks for false, means if first argument false, it wont test second whether it is true or false. || looks for true, means even though first argument false, it test for second. If both are false then false otherwise true. In && case both are true then true otherwise false.

1 Comment

Read again the question. It's not that simple.
2

a && b can't be true if a is false, so if a really is false, there's no need to examine b.

a || b can't be false if a is true, so if a really is true, there's no need to examine b.

They're called short-circuit operators for that reason.

If you're trying to defeat that behavior, use & and | instead.

Comments

0

Hmmm... On the off chance the question is your confusion regarding the && operator evaluating the right-hand operand when the left-hand operand is false, then read this:

Conditional And Operator (Java Language Specification)

Specifically, the first sentence:

The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.

and the last:

Thus, && computes the same result as & on boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.