# || and &&

Discussion in 'C Programming' started by mdh, Aug 15, 2006.

1. ### mdhGuest

One of the things I have yet to fully grasp ( of many things) is when
to use, for example

if (c !=b || c !=e || c !=f)....etc

versus

if ( (c !=b && c !=e && c !=f)

What is the real practical difference between these, and if this is not
a good example of the problem, could someone perhaps give a better
example and explanation.

mdh, Aug 15, 2006

2. ### dcorbitGuest

Use && when all of the conditions must be true.
Use || when any of the conditions can be true.

if (criminal_is_caught && criminal_is_convicted) {
send_to_jail();
}

if (criminal_is_electrocuted || criminal_dies_of_old_age) {
order_coffin();
}

dcorbit, Aug 15, 2006

3. ### jmcgillGuest

The only condition that makes this false, is when all three expressions
are false.
The only condition that makes this true, is when all three expressions
are true.

jmcgill, Aug 15, 2006
4. ### mdhGuest

tks...yes...if the negative is not present, that makes sense....the '!'
seems to muddy the waters for me.

mdh, Aug 15, 2006

Generally this is not what you want. Its almost always true. The only time
its false is if all the variables have the same value....
This is more usually what you are after. It will only be true if "c" does
not match any of the others.
Try some examples in a table:-

Say you want to check for a character NOT being a vowel:-

if ( a != 'A' && a != 'E' && a != 'I' && a != 'O' && a != 'U' ) { /* not a
vowel */ }

so if the variable a contains 'X' , substituting the results of the
compares we get a logical statement:-

if ( true && true && true && true && true )

which as you can see is true, which is what we would hope as 'X' is not a
vowel.
On the other hand if the variable a contains 'E' , substituting the results
of the compares we get a logical statement:-

if ( true && false && true && true && true )

as we have one false, the statement is false, which again is what we hoped
as 'E' is not not a vowel (double negative, E is a vowel)
Note that if we use the logical OR, as in your first example, then both the
above will be true, in both cases....

Thats OK.
Thats OK

6. ### mdhGuest

got it...thanks

mdh, Aug 15, 2006
7. ### Mark McIntyreGuest

if (criminal_is_not_caught || criminal_is_not_convicted)
look_for_more_evidence();

if(criminal_is_not_electrocuted && criminal_does_not_die)
order_more_food();

....
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

Mark McIntyre, Aug 15, 2006
8. ### Frederick GothamGuest

mdh posted:

If you want to change an AND expression into an OR expression, then invert
all operands, and also invert the entire result. Therefore:

a || b || c || d

is equal to:

!( !a && !b && !c && !d )

Similarly, the following:

a && b && c && d

is equal to:

!( !a || !b || !c || !d )

Frederick Gotham, Aug 15, 2006
9. ### dcorbitGuest

Or that
A && ! B || C && D || ( E || A ) && ! ( B && ! C ) || Q && Y && ( ! Q
|| ! A && Q ) && ! ( ! A ) || A

is equivalent to:
Q && Y && A && ( ! A && Q || ! Q ) || ( ! B || C ) && ( A || E ) || C
&& D || ! B && A || A

Not not quite as notty.

dcorbit, Aug 16, 2006
10. ### CBFalconerGuest

For clarity, first add the reduntant parentheses:

if ((c != b) || (c != e) || (c != f))

Now realize that the || (and &&) operators are short circuit
operators, which means that as soon as the expression is no longer
dependent on later terms, those terms are ignored. In this case if
(c != b) that portion evaluates to 1 (non-zero), and no further
terms can affect the net result. Therefore the remaining terms
will not be evaluated IN THIS CASE.

The equivalent analysis of the && case will show that evaluation
ceases when any term evaluates to 0, since nothing can be anded
with a zero to form a non-zero.

CBFalconer, Aug 16, 2006
11. ### mdhGuest

tks ....

mdh, Aug 16, 2006
12. ### Robert LatestGuest

On Tue, 15 Aug 2006 23:02:23 +0100,

robert

Robert Latest, Aug 16, 2006
13. ### Mark McIntyreGuest

Evidently you've never been in a British magistrates court
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

Mark McIntyre, Aug 16, 2006
14. ### Chris F.A. JohnsonGuest

Not at all. If the suspect is not caught or convicted, then
obviously neither is the criminal.

Chris F.A. Johnson, Aug 16, 2006
15. ### lawrence.jonesGuest

What makes it confusing when there are negatives involved is that the
way we say it in English doesn't directly correspond to the way you'd
normally write it. In English, we would normally express a character
not being a vowel as "If the character isn't A, E, I, O, or U". Note
that, in English, we use "or" whereas in the above code we use &&. The
reason is that the English expression actually corresponds to the code:

if (!(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'))

That is, the "not" applies to the whole expression, not the individual
pieces. When you move the "not" to the individual pieces, you also have
to change the "or"s to "and"s.

-Larry Jones

I don't NEED to compromise my principles, because they don't have
the slightest bearing on what happens to me anyway. -- Calvin

lawrence.jones, Aug 16, 2006