|| and &&

M

mdh

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.

Thanks in advance.
 
D

dcorbit

mdh said:
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.

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();
}
 
J

jmcgill

mdh said:
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

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

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

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

mdh

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();
}

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

David Wade

mdh said:
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

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....
versus

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

This is more usually what you are after. It will only be true if "c" does
not match any of the others.
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.

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....

Thanks in advance.

Thats OK.
Thats OK
 
M

Mark McIntyre

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

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
 
F

Frederick Gotham

mdh posted:
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.


Maybe a tutorial in boolean logic would help you.

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 )
 
D

dcorbit

Frederick said:
mdh posted:



Maybe a tutorial in boolean logic would help you.

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 )

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.
 
C

CBFalconer

mdh said:
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.

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.
 
M

mdh

CBFalconer said:
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.


tks ....
 
R

Robert Latest

On Tue, 15 Aug 2006 23:02:23 +0100,
in Msg. said:
if (criminal_is_not_caught || criminal_is_not_convicted)
look_for_more_evidence();

This is contradictory. Make that "suspect" instead of "criminal".

robert
 
M

Mark McIntyre

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


This is contradictory. Make that "suspect" instead of "criminal".

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
 
C

Chris F.A. Johnson

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


This is contradictory. Make that "suspect" instead of "criminal".

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

lawrence.jones

David Wade said:
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 */ }

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top