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?

ragingbull
  • 119
  • 1
  • 2
  • 8
  • 4
    See http://stackoverflow.com/questions/8759868/java-logical-operator-short-circuiting – d.moncada Nov 26 '13 at 03: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? – mellamokb Nov 26 '13 at 03:37
  • See the duplicate, and read about Truth Tables for logical conjunction (&&) and logical disjunction (||) (http://en.wikipedia.org/wiki/Truth_table#Logical_conjunction) –  Nov 26 '13 at 03:40

5 Answers5

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
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.
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • 2
    uhm... read his question again carefully. – Octoshape Nov 26 '13 at 03:37
  • This is **not* what OP asked about. – PM 77-1 Nov 26 '13 at 03:37
  • 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? – ragingbull Nov 26 '13 at 03:38
  • 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 Nov 26 '13 at 03:52
  • 1
    @ragingbull: The best thing might be to delete the question so it doesn't lead to further confusion. – mellamokb Nov 26 '13 at 04:03
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.

Chowdappa
  • 1,580
  • 1
  • 15
  • 31
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.

Chris
  • 121
  • 3
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.