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?
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.
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.
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.
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').
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With