Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between && and || [duplicate]

Tags:

java

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?

like image 919
ragingbull Avatar asked Nov 26 '13 03:11

ragingbull


People also ask

What is the meaning of the phrase difference between?

1 : what makes two or more persons or things not the same I can't see any difference between the two designs. 2 : a disagreement about something They've always had their differences. 3 : the number that is left after subtracting one number from another The difference between six and four is two.

Is it different between or difference between?

different is an adjective and can be used in sentences like : We are both different in terms of our mentality. difference is a noun and can be used in sentences like : The only difference between us is our mentality.

What word means to explain the difference between two things?

You probably know contrast in its relation to compare. To contrast something is to look for differences among two or more elements, but compare is to do the opposite, to look for similarities.

What's the difference between among and between?

Most usage guides will say that you want to use between when referring to two people or things ('sitting between John and Carol') and among when referring to more than two people or things ('students chatting among themselves').


1 Answers

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.

like image 193
Sergey Kalinichenko Avatar answered Oct 08 '22 02:10

Sergey Kalinichenko