473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

|| and &&

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.

Aug 15 '06 #1
14 1341

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

Aug 15 '06 #2
mdh wrote:
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.
Aug 15 '06 #3
mdh

dc*****@connx.com wrote:
mdh wrote:
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();
}
tks...yes...if the negative is not present, that makes sense....the '!'
seems to muddy the waters for me.

Aug 15 '06 #4

"mdh" <md**@comcast.netwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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
Aug 15 '06 #5
mdh

David Wade wrote:
Note that if we use the logical OR, as in your first example, then both the
above will be true, in both cases....
got it...thanks

Aug 15 '06 #6
On 15 Aug 2006 13:15:49 -0700, in comp.lang.c , "mdh"
<md**@comcast.netwrote:
>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
Aug 15 '06 #7
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 )

--

Frederick Gotham
Aug 15 '06 #8
Frederick Gotham wrote:
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 )
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.

Aug 16 '06 #9
mdh wrote:
>
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.

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!
Aug 16 '06 #10
mdh

CBFalconer wrote:
mdh wrote:

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

tks ....

Aug 16 '06 #11
On Tue, 15 Aug 2006 23:02:23 +0100,
Mark McIntyre <ma**********@spamcop.netwrote
in Msg. <90********************************@4ax.com>
if (criminal_is_not_caught || criminal_is_not_convicted)
look_for_more_evidence();
This is contradictory. Make that "suspect" instead of "criminal".

robert
Aug 16 '06 #12
On 16 Aug 2006 12:04:48 GMT, in comp.lang.c , Robert Latest
<bo*******@yahoo.comwrote:
>On Tue, 15 Aug 2006 23:02:23 +0100,
Mark McIntyre <ma**********@spamcop.netwrote
in Msg. <90********************************@4ax.com>
>if (criminal_is_not_caught || criminal_is_not_convicted)
look_for_more_evidence();

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
Aug 16 '06 #13
On 2006-08-16, Robert Latest wrote:
On Tue, 15 Aug 2006 23:02:23 +0100,
Mark McIntyre <ma**********@spamcop.netwrote
in Msg. <90********************************@4ax.com>
>if (criminal_is_not_caught || criminal_is_not_convicted)
look_for_more_evidence();

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.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Aug 16 '06 #14
David Wade <g8***@yahoo.comwrote:
>
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
Aug 16 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.